skip to Main Content

Yesterday I posted a question which is not yet solved.

Here you have a screenshot from the widget

enter image description here

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

enter image description here

But only for a couple of seconds, then it comes back to the state

enter image description here

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


  1. I think your variable getting reinitialised when the ForoProvider() instant created or called.

    class ForoProvider extends ChangeNotifier {
    
    //VARIABLES  
    int numMensajesForoEsp16 = 0;
    int numMensajesForoEng16 = 0; 
    int numMensajesForoEsp16Vistos = 0;
    int numMensajesForoEng16Vistos = 0;
    
    void cambiarNumeroMensajesForo16esp (int s){
      numMensajesForoEsp16 = s;
      notifyListeners();
    }
    . 
    .
    .
    .
    .
    }
    

    Try to initialise the variable above the class like.

    //VARIABLES  
    int numMensajesForoEsp16 = 0;
    int numMensajesForoEng16 = 0; 
    int numMensajesForoEsp16Vistos = 0;
    int numMensajesForoEng16Vistos = 0;
    
    class ForoProvider extends ChangeNotifier {
    ...
    }
    
    Login or Signup to reply.
  2. 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:

    // main_app.dart (or up in your widget tree):
    
    @override
    Widget build(BuildContext context) {
      return ChangeNotifierProvider<ForoProvider>(
          create: (_) => ForoProvider(),
          child: const AppHome(),
        );
    
    // app_home.dart
    @override
    Widget build(BuildContext context) {
      // Watch can be used either in stateless or statefull widgets
      // This lane will make sure to rebuild the widget every time a 
      // notifyListeners is sent.
      final foroProvider = context.watch<ForoProvider>();
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search