flutter 不允许提供程序在初始化期间修改其他提供程序

cl25kdpy  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(91)

我有一个场景,当我在屏幕上加载我的数据时,我想改变加载类的状态。因此,我试图从另一个提供程序切换提供程序的初始状态,但抛出了一个错误。“提供程序在初始化期间不允许修改其他提供程序。”我需要知道处理这种情况的最佳实践。我的
分类如下:

class CleansingServices extends StateNotifier<List<CleansingBaseModel>> {
  CleansingServices() : super([]);

  void setServices(List<CleansingBaseModel> data) {
    state = data;
  }
}

final cleansingServicesProvider = StateNotifierProvider<CleansingServices, List<CleansingBaseModel>>((ref) {
  final data = ref.watch(loadServicesProvider);
  final dataLoading = ref.watch(cleansingLoadingStateProvider.notifier);

  data.when(
    data: (data) {
      ref.notifier.setServices(data);
      dataLoading.setNotLoading();
    },
    error: (error, str) {
      dataLoading.setNotLoadingWithError(error);
    },
    loading: () {
      dataLoading.setLoading();
    },
  );
  return CleansingServices();
});
ars1skjm

ars1skjm1#

class CleansingServices extends StateNotifier<List<CleansingBaseModel>> {
      var data ; 
      CleansingServices(this.data) : super([]){

      data.when(
        data: (data) {
          ref.notifier.setServices(data);
          dataLoading.setNotLoading();
        },
        error: (error, str) {
          dataLoading.setNotLoadingWithError(error);
        },
        loading: () {
          dataLoading.setLoading();
        },
      );

    }
    
      void setServices(List<CleansingBaseModel> data) {
        state = data;
      }
    }
    
    final cleansingServicesProvider = StateNotifierProvider<CleansingServices, List<CleansingBaseModel>>((ref) {
      final data = ref.watch(loadServicesProvider);
      final dataLoading = ref.watch(cleansingLoadingStateProvider.notifier);
    
     
      return CleansingServices(data );
    });

相关问题