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