[英]Using flutter GetX controller for setState()
I want to call the value of the string in my controller, here's my controller:我想调用我的controller中字符串的值,这里是我的controller:
class SplashScreenController extends GetxController {
late String one = _setImage();
@override
void onInit() {
_initPackageInfo();
_setImage();
Timer(Duration(seconds: 5), () => Get.offNamed(Routes.DASHBOARD));
super.onInit();
}
@override
void onClose() {}
PackageInfo _packageInfo = PackageInfo(
appName: 'Unknown',
packageName: 'Unknown',
version: 'Unknown',
buildNumber: 'Unknown',
buildSignature: 'Unknown',
);
Future<void> _initPackageInfo() async {
_packageInfo = await PackageInfo.fromPlatform();
}
String _setImage() {
print(_packageInfo.packageName);
///main package
if (_packageInfo.appName == 'x1') {
return Images.x1;
} else if (_packageInfo.packageName == 'com.package.package1') {
return Images.package1;
} else {
return Images.x1;
}
}
}
I want to call function setImage()
in my widget, but there is an issue after I call it, you can see it on here:我想在我的小部件中调用 function setImage()
,但调用后出现问题,您可以在此处查看:
[Get] the improper use of a GetX has been detected. [Get] 检测到 GetX 的不当使用。 You should only use GetX or Obx for the specific widget that will be updated.您应该只对将要更新的特定小部件使用 GetX 或 Obx。 If you are seeing this error, you probably did not insert any observable variables into GetX/Obx or insert them outside the scope that GetX considers suitable for an update (example: GetX => HeavyWidget => variableObservable).如果您看到此错误,您可能没有将任何可观察变量插入 GetX/Obx 或将它们插入到 GetX 认为适合更新的 scope 之外(例如:GetX => HeavyWidget => variableObservable)。 If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.如果您需要更新父小部件和子小部件,请将每个小部件包装在 Obx/GetX 中。
And here is the view, where I call the controller function:这是视图,我在这里调用 controller function:
Widget build(BuildContext context) {
return Obx(() => Scaffold(
body: Container(
color: const Color.fromARGB(255, 255, 255, 255),
alignment: AlignmentDirectional.center,
child: Image.asset(controller.one)),
));
}
is there any way to fix it so I can access _setImage()
on my view?有什么方法可以修复它,以便我可以在我的视图中访问_setImage()
吗?
You have this error because you wrap your code with Obx but you don't use an observable variable inside.你有这个错误是因为你用 Obx 包装你的代码但你没有在里面使用可观察变量。
Change your code like this:像这样更改您的代码:
class SplashScreenController extends GetxController {
late String one = ''.obs; // This is the observable variable that you need
@override
void onInit() {
_initPackageInfo();
_setImage();
Timer(Duration(seconds: 5), () => Get.offNamed(Routes.DASHBOARD));
super.onInit();
}
@override
void onClose() {}
PackageInfo _packageInfo = PackageInfo(
appName: 'Unknown',
packageName: 'Unknown',
version: 'Unknown',
buildNumber: 'Unknown',
buildSignature: 'Unknown',
);
Future<void> _initPackageInfo() async {
_packageInfo = await PackageInfo.fromPlatform();
}
void _setImage() {
print(_packageInfo.packageName);
///main package
if (_packageInfo.appName == 'x1') {
one = Images.x1;
} else if (_packageInfo.packageName == 'com.package.package1') {
one = Images.package1;
} else {
one = Images.x1;
}
}
}
And for the Widget:对于小部件:
Widget build(BuildContext context) {
return Obx(() => Scaffold(
body: Container(
color: const Color.fromARGB(255, 255, 255, 255),
alignment: AlignmentDirectional.center,
child: controller.one.isNotEmpty
? Image.asset(controller.one))
: const SizedBox.shrink(),
));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.