0
Hello!
I’m trying to make a checkbox list (Checkboxlisttile) using mobx, but every time I click on the box to check/uncheck I have to use the flutter reload
or the setState
to update the data.
mobx is not automatically updating using Observer. I don’t know why. Could you help me?
My model was like this:
abstract class _ItemComplementoModel with Store{
@observable
ObservableList<ItemComplemento> listaItens = ObservableList<ItemComplemento>();
@action
void addItem(ItemComplemento novoItem){
listaItens.add(novoItem);
}
@action
void marcarItem(int indice, bool marcou, CarrinhoModel carrinhoModel){
ItemComplemento itemComplemento = listaItens[indice];
print(itemComplemento.nome);
itemComplemento.marcado = marcou;
}
}
And my Observer:
_parteItemComplemento(int indice, ItemComplemento itemComplemento){
return Observer(
builder: (_){
return CheckboxListTile(
onChanged: (marcou){
itemComplementoModel.marcarItem(indice, marcou, carrinhoModel);
print("marcou ${marcou}");
},
title: Text(itemComplemento.nome),
value: itemComplementoModel.listaItens[indice].marcado,
secondary: CircleAvatar(
child: Icon(
itemComplementoModel.listaItens[indice].marcado
? Icons.check
: Icons.error
),
),
);
},
);
}
Have you tried the same procedure I indicated in your other question? Within the function
marcarItem()
, do everything necessary with the item and in the end dolistaItens[indice] = itemComplemento
? It’s not the best approach but it should work.– Matheus Ribeiro
Yes, I tried to do this way, the problem is that the list does not update the same as the one using Map.
– Alex Anderson
That worked, apparently not this. I found it strange! Indicates me some other approach to try?
– Alex Anderson
I advise you to take a look at a repository of mine from Github Campo Minado. There I work with a similar scheme, you will be able to find an example in the files Fieldmodel and Minesweepercontroller
– Matheus Ribeiro
I took a look, but I couldn’t come up with a solution to this problem.
– Alex Anderson