状态更新时消费者小部件不会重建 UI

she*_*r s 5 state-management dart flutter riverpod

我正在使用 flutter Riverpod(State Notifier Provider)进行状态管理。当我更新状态时,消费者小部件不会重建自身,因此即使我的状态正在更新,UI 也不会更新。我无法调试原因。

Pubspec.yaml

   flutter_riverpod: ^0.14.0+3
Run Code Online (Sandbox Code Playgroud)

Riverpod StateNotifier 提供商

    final dataListModelProvider=StateNotifierProvider((ref)=>ExampleProvider ([]));
Run Code Online (Sandbox Code Playgroud)

供应商


  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);
      }
   }
}

Run Code Online (Sandbox Code Playgroud)

Riverpod 消费者小部件

            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),
                        );
                      }
                    });
              }),
Run Code Online (Sandbox Code Playgroud)

更新状态的按钮

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

Run Code Online (Sandbox Code Playgroud)

控制台输出在初始状态上构建时(启动屏幕时)

  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']

Run Code Online (Sandbox Code Playgroud)

调用创建函数后(更新状态时)

  ===== 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']
   
Run Code Online (Sandbox Code Playgroud)