首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mbox未刷新对象

Mbox未刷新对象
EN

Stack Overflow用户
提问于 2021-09-01 18:22:31
回答 2查看 34关注 0票数 0

我正在使用Mbox制作一个屏幕,但是它并没有刷新我的对象。

我只想在创建一个包含此信息的表之后显示一个字符串列表。

我的Mbox代码是:

代码语言:javascript
复制
abstract class ControllerResearchBase with Store {

Pesquisa _finalResearch = new Pesquisa();

  set finalResearch(Pesquisa pesquisa) {
    _finalResearch = pesquisa;
  }

  @observable
  List<String> informationLC = [];

  @action
  void changeLojaOrConcorrete({int op = 0}) {
    informationLC.clear();
    if (_finalResearch == null) informationLC.add("Error, pesquisa vazia");
    if (op == 0) {
      if (_finalResearch.loja == null) {
        informationLC.add("Error, loja vazia");
        return;
      }
      _finalResearch.loja.forEach((element) {
        informationLC.add(element.desLoja);
      });
    } else if (op == 1) {
      if (_finalResearch.concorrente == null) {
        informationLC.add("Error, concorrente vazia");
        return;
      }
      _finalResearch.concorrente.forEach((element) {
        informationLC.add(element.desConco);
      });
    } else {
      informationLC.add("Opção invalida");
    }
  }

我已经创建了类ControllerResearch和file.g.dart

简单的屏幕是:

代码语言:javascript
复制
    Widget build(BuildContext context) {
    ControllerResearch controller = new ControllerResearch();
    UtilsWidgetsComponents _components = new UtilsWidgetsComponents();
    controller.finalResearch = finalResearch;

    return Scaffold(
        appBar: AppBar(
          flexibleSpace: _components.colorAppBar(),
          title: Center(child: Text('Detalhes')),
        ),
        body: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ElevatedButton(
                      onPressed: () {
                        controller.changeLojaOrConcorrete(op: 0);
                      },
                      child: Text("Lojas")),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ElevatedButton(
                      onPressed: () {
                        controller.changeLojaOrConcorrete(op: 1);
                      },
                      child: Text("Concorrentes")),
                ),
              ],
            ),
            Observer(builder: (_) => Text("${controller.informationLC}"))
          ],
        ),
      ),
   );
}

在我的打印调试中,信息是正确的,但是在屏幕上显示[]

EN

回答 2

Stack Overflow用户

发布于 2021-09-01 18:30:33

好吧,问题可能是每次调用build时都会重新启动控制器:

代码语言:javascript
复制
Widget build(BuildContext context) {
   ControllerResearch controller = new ControllerResearch();
   UtilsWidgetsComponents _components = new UtilsWidgetsComponents();
   controller.finalResearch = finalResearch;

如果你把你的变量移到build方法之外,它可以解决这个问题。

票数 1
EN

Stack Overflow用户

发布于 2021-09-01 20:18:38

我将我的Mbox代码更改为:

代码语言:javascript
复制
  @action
  Future changeLojaOrConcorrete({int op = 0}) async {
    //-1 == invalid
    //0 == loja
    //1 == concorrente
    List<String> saida = [];
    informationLC = [];
    if (_finalResearch == null) saida.add("Error, pesquisa vazia");
    if (op == 0) {
      if (_finalResearch.loja == null) {
        saida.add("Error, loja vazia");
      }

      _finalResearch.loja.forEach((element) {
        saida.add(element.desLoja);
      });
    } else if (op == 1) {
      if (_finalResearch.concorrente == null) {
        saida.add("Error, concorrente vazia");
      }
      _finalResearch.concorrente.forEach((element) {
        saida.add(element.desConco);
      });
    } else {
      saida.add("Opção invalida");
    }
    informationLC = saida;
  }

当我将information.add( 'something‘)改为information =’something‘时,它起作用了。显然,Mobx @oberservable不能很好地与List<>方法或类似的方法一起工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69018862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档