Flutter Mobx在构建器函数中未检测到可观察到的东西

时间:2019-07-15 14:33:29

标签: flutter mobx

嗨,我对测试加载状态很简单。

abstract class _AccountStore with Store {
  @observable
  bool loadingButtonStatus = false;


  @observable
  bool get loading => loadingButtonStatus;


  @action
  Future updateAccount(formData) async {
    loadingButtonStatus = true;

    Future.delayed(Duration(milliseconds: 2000)).then((future) {
      loadingButtonStatus = false;
    }).catchError((e) {
      loadingButtonStatus = false;
      print(e);
    });
  }
}

这是我的小部件

AccountStore store = AccountStore();

Observer(
  name: 'loading_button',
  builder: (_) => LoadingButton(
        loading: store.loading,
        text: Text('Save'),
        onPressed: () {
          store.updateAccount({});
        },
))

但是每次我运行代码时,它总会返回我:There are no observables detected in the builder function

我尝试使用store.loadingButtonStatus进行更改,但仍然相同。

任何解决方案?

谢谢。

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,但以上答案对我不起作用,而且代码也是正确的。 我的 part 没有更新。

只需在终端中运行此命令

flutter packages pub run build_runner build

我希望这会奏效。

答案 1 :(得分:0)

如果使用吸气剂,则必须使用@computed。

  @observable
  bool _loadingButtonStatus = false;


  @computed
  bool get loading => _loadingButtonStatus;

答案 2 :(得分:0)

我也看到过这个错误。因为 store_name.g.dart 没有更新。

只需在终端中运行 scrip flutter pub run build_runner watch --delete-conflicting-outputs