5

I am using flutter riverpod(State Notifier Provider) for state management. When I am updating the state the consumer widget is not rebuilding itself because of which the UI is not getting updated even though my state is getting updated. I am unable to debug the reason.

Pubspec.yaml

   flutter_riverpod: ^0.14.0+3

Riverpod StateNotifier Provider

    final dataListModelProvider=StateNotifierProvider((ref)=>ExampleProvider ([]));

PROVIDER


  class ExampleProvider extends StateNotifier<List<Example>>{

     ExampleProvider (List<Example> state):super(state ?? []);

     void createExample(Map exampleData) async{
     try{
        var response= await ExampleDao().createExampleData(exampleData);      // API request - working fine( All the data is coming back from API as expected)
       print(" ===== CREATED EXAMPLE TYPE===========");
       Example example=response;
       List<Example> listExampleData= [...state,example];
       print("========== INITIAL STATE =============Count = ${state.length}");
       print(state);
       state=listExampleData;
       print("========== UPDATED STATE =============Count = ${state.length}");
       print(state);
     }
    }catch(error,stackTrace){
         print(error);
         print(stackTrace);
      }
   }
}

Riverpod Consumer widget

            Consumer(builder: (context,watch,child){
                print("STEP-1");
                dynamic value=context.read(dataListModelProvider.notifier).state;
                print("STEP-2");
                print(value);
                return FutureBuilder(
                    future: watch(dataFetchDataProvider),
                    builder: (context,snapshot){
                      if(snapshot.connectionState==ConnectionState.done){
                        print("######## SNAPSHOT VALUE ############");
                        print(snapshot.data);
                        return mainUI(size, _width, _height, isNull(value) || 
                        value.length==0?snapshot.data:value);
                      }
                      else{
                        return Center(
                          child: CircularProgressIndicator(backgroundColor: 
                              SolidColor.colorWhite),
                        );
                      }
                    });
              }),

Button which updates states

          RaisedButton(
              onPressed:context.read(dataListModelProvider.notifier).createExample(inputListData),
              child: Text('CLICK HERE'),
            )

Console Output When builds on initial State (When launching Screen)

  STEP-1
  STEP-2
  []
  ######## SNAPSHOT VALUE ############
  [Instance of 'Example', Instance of 'Example', Instance of 'Example', Instance of 
  'Example', Instance of 'Example', Instance of 'Example', Instance of 'Example']

After create function is called (while updating state)

  ===== CREATED EXAMPLE TYPE==========="
  ============ Initial State ================ Count = 7
  [Instance of 'Example', Instance of 'Example', Instance of 'Example', Instance of 
    'Example', Instance of 'Example', Instance of 'Example', Instance of 'Example']
  ============ State Updated ================== Count = 8
  [Instance of 'Example', Instance of 'Example', Instance of 'Example', Instance of 'Example', Instance of 'Example', Instance of 'Example', Instance of 'Example', Instance of 'Example']
   
shekhar s
  • 51
  • 1
  • 3

0 Answers0