【问题标题】:[Flutter][Getx][State Management] using StateManagement[Flutter][Getx][State Management] 使用 StateManagement
【发布时间】:2021-10-10 04:13:10
【问题描述】:

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

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

 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 方法)

   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 中插入任何可观察变量”

感谢阅读。

【问题讨论】:

  • * 在 DragTarget 小部件中它包含 banList 变量
  • 根据您分享的内容,您有 2 个可观察变量。两者都不在您的 Obx 中,因此该错误非常清楚。 Obx 需要一个可观察变量,而您没有插入。

标签: list flutter dart state-management flutter-getx


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-08-04
    • 2021-04-16
    • 2021-03-06
    • 2022-12-14
    • 2022-01-08
    • 2021-05-03
    • 2021-04-06
    • 2021-07-09
    • 2021-08-09
    相关资源
    最近更新 更多