Yesterday I posted a question which is not yet solved.
Here you have a screenshot from the widget
The values 0 / 5 are
provider.numMensajesForoEsp16Vistos = 0
provider.numMensajesForoEsp16 = 5
Here you have the code for the widget at bottom right
child: Row(mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("${provider.numMensajesForoEsp16Vistos} / ${provider.numMensajesForoEsp16}",
style: TextStyle(fontSize: 16, color: hexStringToColor( especialidad .color_especialidad))),
Padding(
padding: const EdgeInsets.all(8.0),child: Visibility(
visible: provider.numMensajesForoEsp16 >
provider.numMensajesForoEsp16Vistos,
child: Row( mainAxisAlignment: MainAxisAlignment.end,
children: [Container(width: 15, height: 15,
decoration: BoxDecoration(
color: Colors.red, shape: BoxShape .circle), ), ], )
When clicking on the card, here you have the code:
if (_esEsp) {
print("nume mensajes descargados ${provider.numMensajesForoEsp16}");
provider.cambiarNumeroMensajesForo16espVistos(provider.numMensajesForoEsp16);
}
if (!_esEsp) {provider.cambiarNumeroMensajesForo16engVistos(provider.numMensajesForoEng16);
}
That code should update cambiarNumeroMensajesForo16espVistos to 5, as it is shown in the print output:
flutter: nume mensajes descargados 5
After clicking the card, the app opens another screen, and the if you click to go back to previous screen, the widgets is updated to the new values
But only for a couple of seconds, then it comes back to the state
Here you have the provider file:
class ForoProvider extends ChangeNotifier {
//VARIABLES
int numMensajesForoEsp16 = 0;
int numMensajesForoEng16 = 0;
int numMensajesForoEsp16Vistos = 0;
int numMensajesForoEng16Vistos = 0;
void cambiarNumeroMensajesForo16esp (int s){
numMensajesForoEsp16 = s;
notifyListeners();
}
void cambiarNumeroMensajesForo16eng (int s){
numMensajesForoEng16 = s;
notifyListeners();
}
//vistos
void cambiarNumeroMensajesForo16espVistos (int s){
numMensajesForoEsp16Vistos = s;
notifyListeners();
}
void cambiarNumeroMensajesForo16engVistos(int s){
numMensajesForoEng16Vistos = s;
notifyListeners();
}
And here you have to top widget that includes Consumer
Expanded(
child: RefreshIndicator(
onRefresh: refresh,
child: Consumer<ForoProvider>(
builder: (context, provider, child) {
return Column(
I don´t know what is wrong or missing in order to permanently update the value for
provider.numMensajesForoEsp16Vistos
2
Answers
I think your variable getting reinitialised when the ForoProvider() instant created or called.
Try to initialise the variable above the class like.
It seems to me (without access of the full code) that:
1 – the value resets: you might be creating your provider in the widget that generates the 1st card. This way, when you go back to it, the values are reseted.
2 – the value doesn’t update: for the widget to update when the provider notifies, it has to be listening to the provider (context.watch, context.select or listen using AnimatedContainer). Alternatively you could use a setState after calling the provider.
Test sth like this: