美文网首页
flutter You should only use GetX

flutter You should only use GetX

作者: 一个冬季 | 来源:发表于2023-12-06 22:52 被阅读0次

    错误日志

     [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类型导致的
    }
    

    相关文章

      网友评论

          本文标题:flutter You should only use GetX

          本文链接:https://www.haomeiwen.com/subject/dxqsgdtx.html