Mbox isn't refreshing the object

110 views Asked by At

I'm making a screen using Mbox, however it aren't refresh my object.

I wanna just show a List of string for after create a table with this information.

My Mbox code is it:

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");
    }
  }

I already create the class ControllerResearch and file.g.dart

The simple screen is:

    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}"))
          ],
        ),
      ),
   );
}

In my prints debug, the information is correct, However at screen is showing []

2

There are 2 answers

12
Gabriel Reis On

Okay, the problem could be that you are restarting your controller every time build is called:

Widget build(BuildContext context) {
   ControllerResearch controller = new ControllerResearch();
   UtilsWidgetsComponents _components = new UtilsWidgetsComponents();
   controller.finalResearch = finalResearch;

If you move your variables to outside of build method, it could fix the issue.

0
Marcos Gianetti On

I changed my Mbox code for:

  @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;
  }

When I changed information.add('something') for information = ['something'], it worked. Apparently, Mobx @oberservable don't work very well with methods List<>, or like that.