在GETX对话框中使用GETX OBX和GETX状态管理

发布于 2025-02-09 18:52:21 字数 1070 浏览 1 评论 0原文

我在应用程序中使用GET对话框,我想在对话框内部使用GetX状态管理,例如我在对话框内有复选框,并且我想使用GetX更新其状态,但是当我使用getx< appcontroller>( )在我的获取对话框中,我遇到了以下错误

您只应将getx或obx用于特定的小部件 更新。 如果您看到此错误,您可能没有将任何可观察的变量插入GetX/obx 或将它们插入getx认为适合更新的范围之外 (示例:getx => heidewidget => variable observable)。 如果您需要更新父窗口小部件和子小部件,请将每个小部件包装在OBX/GETX中。 这是示例代码:

Get.dialog(
      Dialog(
        child: Flexible(
            child: GetX<AppController>(
              init: appController,
              builder: (val){
                return CheckboxListTile(
                  title: Text("Power off computer"),
                  contentPadding: EdgeInsets.zero,
                  value: appController.powerOffRaspberry,
                  onChanged: (newValue) {
                    appController.powerOffRaspberry = !appController.powerOffRaspberry;
                  },
                  controlAffinity: ListTileControlAffinity.leading, //  <-- leading Checkbox
                );
              },
            )
        ),
      ),

    )

I am using Get dialog in my app and I want to use GetX state management inside my dialog widget for example I have checkbox inside my dialog and I want to update it's state with GetX however when I use the GetX<AppController>() inside my get dialog I ran into following error

You should only use GetX or Obx for the specific widget that will be
updated.
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).
If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.
Here is the sample code:

Get.dialog(
      Dialog(
        child: Flexible(
            child: GetX<AppController>(
              init: appController,
              builder: (val){
                return CheckboxListTile(
                  title: Text("Power off computer"),
                  contentPadding: EdgeInsets.zero,
                  value: appController.powerOffRaspberry,
                  onChanged: (newValue) {
                    appController.powerOffRaspberry = !appController.powerOffRaspberry;
                  },
                  controlAffinity: ListTileControlAffinity.leading, //  <-- leading Checkbox
                );
              },
            )
        ),
      ),

    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2025-02-16 18:52:21

制作AppController.poweroffraspberry可观察:

final powerOffRaspberry = false.obs; 

update chackboxlisttile as:

CheckboxListTile(
              title: Text("Power off computer"),
              contentPadding: EdgeInsets.zero,
              value: appController.powerOffRaspberry.value,
              onChanged: (newValue) {
                appController.powerOffRaspberry.value = !appController.powerOffRaspberry.value;
              },
              controlAffinity: ListTileControlAffinity.leading, //  <-- leading Checkbox
            )

Make appController.powerOffRaspberry observable:

final powerOffRaspberry = false.obs; 

Update CheckboxListTile as:

CheckboxListTile(
              title: Text("Power off computer"),
              contentPadding: EdgeInsets.zero,
              value: appController.powerOffRaspberry.value,
              onChanged: (newValue) {
                appController.powerOffRaspberry.value = !appController.powerOffRaspberry.value;
              },
              controlAffinity: ListTileControlAffinity.leading, //  <-- leading Checkbox
            )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文