Naveed Ullah
Naveed Ullah

Reputation: 145

Providers are not allowed to modify other providers during their initialization

I have a scenario, where I want to change state of loading class while I load my data on screen. So for that I am trying to switch the initial state of a provider from another provider but throws me an error. "Providers are not allowed to modify other providers during their initialisation." I need to know the very best practice to handle this kind of scenarios. My

classes are as follow:

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

Upvotes: 3

Views: 2662

Answers (1)

Erfan Eghterafi
Erfan Eghterafi

Reputation: 5645

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

Upvotes: 1

Related Questions