flutter You should only use GetX or Obx for the specific widget that will be updated

文章讲述了在使用GetX和Obx时遇到的错误,即试图监听不属于Rx类型的数据。解决方法是将`isSelect`属性改为`RxBool`类型。作者提供了在Flutter中处理此类错误和转换数据类型的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误日志
 [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.
错误代码
class _GridViewExtent extends GetView<GenerateResultController> {
  const _GridViewExtent({super.key});

  @override
  Widget build(BuildContext context) {
    return GridView.extent(
      .........
      children: initGridCount(),
    );

List<Widget> initGridCount() {
    List<Widget> list = [];
    for (int i = 0; i < controller.listGenBean.value.length; i++) {
      GeneratedResultModel generatedResultModel = controller.listGenBean.value[i];
      list.add(GestureDetector(
        onTap: () {
           ....
        },
        child: Stack(
          children: [
            Obx(() => Positioned(
                   ...
                child: Offstage(
                  offstage: !generatedResultModel.isSelect,//错误问题就在这里,我想监听的这个参数,不属于RxBool类型
                   ....,
                )))
          ],
        ),
      ));
    }
    return list;
  }

class GenerateResultController extends GetxController{
  RxList<GeneratedResultModel> listGenBean = <GeneratedResultModel>[].obs;
}

class GeneratedResultModel{
   bool isSelect = false; //原因是这个不是RxBool类型导致的
}

简单来说,你使用了Obx进行监听,但是你这监听的数据里面没有任何一个参数属于RxString或者RxBool等的类型

解决方案
class GeneratedResultModel{
   RxBoolisSelect = false.obs; //这个不是RxBool类型导致的
}
我的博客

简书
csdn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值