如何在Flutter中使用Mobx作为List对象?

问题描述 投票:0回答:1

我想通过使用Mobx更改复选框的状态,单击后值已更改但未在UI上更新,观察者看不到正在重建此小部件的更改操作在Mobx文件中:

  @observable
  List<bool> statusCheckbox = List<bool>();

  @computed
  List<bool> get getStatusCheckBox {
  return statusCheckbox;
 }

 @action
 changeStatusItem(bool val, int index) {
 statusCheckbox[index] = val;
 }

在文件中包含小部件:

Observer(
                 name: 'ListHomePage',
                 builder: (BuildContext context) {
                 return  child: GridView.builder(
                              gridDelegate:
                                  new SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: 3,
                                childAspectRatio: (itemWidth  / itemHeight),
                              ),
                              itemCount:
                              _userManagementStore.userAPI.users.length,
                              itemBuilder: (context, index) {
                                UserManagement user =
                                _userManagementStore.getUser(index: index);
                                return AnimationConfiguration.staggeredGrid(
                                  position: index,
                                  duration:
                                      const Duration(milliseconds: 375),
                                  columnCount: 2,
                                  child: Container(
                                    child: ScaleAnimation(
                                      child: GestureDetector(
                                        child: Stack(
                                          children: <Widget>[
                                             Align(
                                              alignment: Alignment.topRight,
                                              child: Checkbox(
                                                value: _userManagementStore.getStatusCheckBox[index],
                                                onChanged: (bool val) {

                             _userManagementStore.changeStatusItem(val, index);

                                                },
                                              ),
                                            ),
                                          ],
                                        ),
                  },
                )

我找不到问题的根本原因

flutter mobx
1个回答
0
投票

如Antoan Elenkov所说,您应该删除@computed属性。但是您的主要问题是您不应该使用List而是ObservalbleList,它的工作方式类似于List。因此,您必须替换

  List<bool> statusCheckbox = List<bool>();

作者

  @observable
  ObservableList<bool> statusCheckbox = ObservableList();

© www.soinside.com 2019 - 2024. All rights reserved.