Search code examples
flutterriverpod

Flutter riverpod, override default async value to my new state


I am using Riverpod and I want to use AsyncNotifier, In this provider is used Async value as a default state, I have created my State and I want to use my state instead of the default one.

How can I use my state instead of the default async value notifier?

@riverpod
class ExploreController extends _$ExploreController {
  @override
  Future<UIState<ExploreModel>> build() async {
    final result = await getExploreModel();
    return UIState.success(data: result);
  }

Solution

  • You can disable conversion of a Stream/Future to AsyncValue using the Raw typedef.

    Raw<Future<UIState<ExploreModel>>> build() async {
      // ...
    }
    

    Please note that it is generally discouraged to disable the AsyncValue conversion.


    More about the Raw typedef: https://riverpod.dev/docs/essentials/websockets_sync#disabling-conversion-of-streamsfutures-to-asyncvalue