list - [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 中插入任何可观察的变量”
谢谢阅读。
解决方案
您必须使用 RxList 进行 obserble 然后它将在 obx 中工作,否则您必须使用 getbuilder 并调用 update() 进行 ui 更改
推荐阅读
- linux - 自动从选项列表中选择选项而不输入值
- docker - 在 helm install 之前将 go 二进制文件移动到 pod 的惯用方法是什么?
- vue.js - 两行 vuetify v-switch 标签
- apache - 无法通过 IP 限制对 SSL 上的 Apache 2.4 反向代理的访问
- oracle - 根据另一个表的总计数动态创建表
- python-3.x - 如何检查列中的值变化
- machine-learning - 机器学习模型是否可能具有低测试误差和高训练误差?
- c# - 如何将用户设置保存到文本文件,然后让应用程序设置文件读取和使用它们
- ubuntu - 安装 curl 后出现致命错误:调用未定义函数 curl_init()
- javascript - 使用 Puppeteer 时在下拉菜单中找不到可用的选择器?