首页 > 解决方案 > [Flutter][Getx][State Management] 使用 StateManagement

问题描述

我正在尝试应用 Getx 状态管理来更新我的屏幕(也许是响应式的?),
我已经阅读了官方的 getx github 文档。这就是我尝试过的。

1.设置我想让它响应.obs的变量

<例如1>

 List blueBan = List<String>.filled(5, champIcon, growable: false).obs;  

 List redBan = List<String>.filled(5, champIcon, growable: false).obs;  

==================================================== =================================================

2. 将 Widget 设置为我想要更新的 Obx(()=> Widget) (更新就像使用 setState 方法)

<例如2>

   Widget banContainer(List banList, String team, int n) {

      return Container(
      ...

          child: Obx(

            () => DragTarget<String>(

              onWillAccept: (value) {

                banTemp = value;

                return true;  

*变量banList 是blueBan,redBan 的引用。
我像banContainer(blueBan, 'blue', 0)一样使用它

↓ 这是我收到的错误消息。

════════ Exception caught by widgets library ═══════════════════════════════════
The following message was thrown building Obx(has builder, dirty, state: _ObxState#1202f):
      [Get] the improper use of a GetX has been detected. 
      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.

The relevant error-causing widget was
Obx
lib\…\ui\BanPick.dart:230
When the exception was thrown, this was the stack
#0      RxInterface.notifyChildren
package:get/…/rx_core/rx_interface.dart:29
#1      _ObxState.build
package:get/…/rx_flutter/rx_obx_widget.dart:54
#2      StatefulElement.build
package:flutter/…/widgets/framework.dart:4691
#3      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4574
#4      StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4746
...
════════════════════════════════════════════════════════════════════════════════

尽管如此,我在我的 List 变量上插入了 .obs,它说“如果你看到这个错误,你可能没有在 GetX/Obx 中插入任何可观察的变量”

谢谢阅读。

标签: listflutterdartstate-managementflutter-getx

解决方案


您必须使用 RxList 进行 obserble 然后它将在 obx 中工作,否则您必须使用 getbuilder 并调用 update() 进行 ui 更改


推荐阅读