Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How do I create an abstract or base Notifier class in Riverpod?

$
0
0

I have multiple notifiers with similar search and other logic, the only difference being the API call. How can I abstract that logic out? And reuse my Notifier in multiple places?Example :

MyNotifierOne

@riverpodclass MyNotifierOne extends _$MyNotifierOne {  @override  Future<List<SuggestionInfo>> build() async {    return networkClient.getNotifierOneAPI(query: searchQuery,page: 1);  }  final debounceSearch = Debounce(milliseconds: 500);  var searchQuery = '';  void onSearch(Ref ref, String query) {    searchQuery = query;    debounceSearch.run(ref.invalidateSelf);  }  String? getSuggestionLogo(String? name){}  SuggestionInfo getSuggestionInfo(String? name){}}

MyNotifierTwo

@riverpodclass MyNotifierTwo extends _$MyNotifierTwo {  @override  Future<List<SuggestionInfo>> build(String? metadata) async {    return networkClient.getNotifierTwoAPI(query: searchQuery, metadata);  }  final debounceSearch = Debounce(milliseconds: 500);  var searchQuery = '';  void onSearch(Ref ref, String query) {    searchQuery = query;    debounceSearch.run(ref.invalidateSelf);  }  String? getSuggestionLogo(String? name){}  SuggestionInfo getSuggestionInfo(String? name){}}

MyNotifierThree

@riverpodclass MyNotifierThree extends _$MyNotifierThree {  @override  Future<List<SuggestionInfo>> build() async {    return networkClient.getNotifierThirdAPI(query: searchQuery);  }  final debounceSearch = Debounce(milliseconds: 500);  var searchQuery = '';  void onSearch(Ref ref, String query) {    searchQuery = query;    debounceSearch.run(ref.invalidateSelf);  }  String? getSuggestionLogo(String? name){}  SuggestionInfo getSuggestionInfo(String? name){}}

Initially, I thought of creating a mixin on abstract class AsyncNotifierProviderBase<NotifierT extends AsyncNotifierBase<T>, but it’s internal and I can’t access it.

And help or resources are appreciated. Thank you


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>