我正在使用Mbox制作一个屏幕,但是它并没有刷新我的对象。
我只想在创建一个包含此信息的表之后显示一个字符串列表。
我的Mbox代码是:
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
简单的屏幕是:
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}"))
],
),
),
);
}在我的打印调试中,信息是正确的,但是在屏幕上显示[]
发布于 2021-09-01 18:30:33
好吧,问题可能是每次调用build时都会重新启动控制器:
Widget build(BuildContext context) {
ControllerResearch controller = new ControllerResearch();
UtilsWidgetsComponents _components = new UtilsWidgetsComponents();
controller.finalResearch = finalResearch;如果你把你的变量移到build方法之外,它可以解决这个问题。
发布于 2021-09-01 20:18:38
我将我的Mbox代码更改为:
@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<>方法或类似的方法一起工作。
https://stackoverflow.com/questions/69018862
复制相似问题