AsyncValue<ValueT> class sealed Core

A utility for safely manipulating asynchronous data.

By using AsyncValue, you are guaranteed that you cannot forget to handle the loading/error state of an asynchronous operation.

AsyncValue is a sealed class, and is designed to be used with pattern matching to handle the different states:

/// A provider that asynchronously exposes the current user
final userProvider = StreamProvider<User>((_) async* {
  // fetch the user
});

class Example extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final AsyncValue<User> user = ref.watch(userProvider);

    return switch (user) {
      AsyncValue(hasError: true) => Text('Oops, something unexpected happened'),
      AsyncValue(:final value, hasValue: true) => Text('Hello ${value.name}'),
      _ => CircularProgressIndicator(),
    );
  }
}

If a consumer of an AsyncValue does not care about the loading/error state, consider using requireValue to read the state:

Widget build(BuildContext context, WidgetRef ref) {
  // Reading .requiredValue will be throw both on loading and error states.
  final User user = ref.watch(userProvider).requiredValue;

  ...
}

By using requireValue, we get an immediate access to the value. At the same time, if we made a mistake and the value is not available, we will get an exception. This is a good thing because it will help us to spot problem.

See also:

Available extensions
Annotations
  • @sealed
  • @immutable
  • @publicInRiverpodAndCodegen

Constructors

AsyncValue.data(ValueT value)
Creates an AsyncValue with a data.
const
factory
AsyncValue.error(Object error, StackTrace stackTrace)
Creates an AsyncValue in the error state.
const
factory
AsyncValue.loading({num progress})
Creates an AsyncValue in loading state.
const
factory

Properties

asData AsyncData<ValueT>?

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Upcast AsyncValue into an AsyncData, or return null if the AsyncValue is an AsyncLoading/AsyncError.
no setter
asError AsyncError<ValueT>?

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Upcast AsyncValue into an AsyncError, or return null if the AsyncValue is an AsyncLoading/AsyncData.
no setter
error Object?
The error.
no setter
hasError bool

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Whether error is not null.
no setter
hashCode int
The hash code for this object.
no setteroverride
hasValue bool

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Whether value is set.
no setter
isFromCache bool

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Whether the value was obtained using Riverpod's offline-persistence feature.
no setter
isLoading bool

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Whether some new value is currently asynchronously loading.
no setter
isRefreshing bool

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Whether the associated provider was forced to recompute even though none of its dependencies has changed, after at least one value/error was emitted.
no setter
isReloading bool

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Whether the associated provider was recomputed because of a dependency change (using Ref.watch), after at least one value/error was emitted.
no setter
progress num?

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

The current progress of the asynchronous operation.
no setter
requireValue → ValueT
If hasValue is true, returns the value. Otherwise if hasError, rethrows the error. Finally if in loading state, throws a StateError.
no setter
retrying bool

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

If an error was emitted, whether that error is currently being retried.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stackTrace StackTrace?
The stacktrace of error.
no setter
value → ValueT?
The value currently exposed.
no setter

Methods

map<NewT>({required NewT data(AsyncData<ValueT> data), required NewT error(AsyncError<ValueT> error), required NewT loading(AsyncLoading<ValueT> loading)}) → NewT

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Perform some action based on the current state of the AsyncValue.
mapOrNull<NewT>({NewT? data(AsyncData<ValueT> data)?, NewT? error(AsyncError<ValueT> error)?, NewT? loading(AsyncLoading<ValueT> loading)?}) → NewT?

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Perform some actions based on the state of the AsyncValue, or return null if the current state wasn't tested.
maybeMap<NewT>({NewT data(AsyncData<ValueT> data)?, NewT error(AsyncError<ValueT> error)?, NewT loading(AsyncLoading<ValueT> loading)?, required NewT orElse()}) → NewT

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Perform some actions based on the state of the AsyncValue, or call orElse if the current state was not tested.
maybeWhen<NewT>({bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, NewT data(ValueT data)?, NewT error(Object error, StackTrace stackTrace)?, NewT loading()?, required NewT orElse()}) → NewT

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Switch-case over the state of the AsyncValue while purposefully not handling some cases.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override
unwrapPrevious() AsyncValue<ValueT>

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

The opposite of copyWithPrevious, reverting to the raw AsyncValue with no information on the previous state.
when<NewT>({bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, required NewT data(ValueT data), required NewT error(Object error, StackTrace stackTrace), required NewT loading()}) → NewT

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Performs an action based on the state of the AsyncValue.
whenData<NewT>(NewT cb(ValueT value)) AsyncValue<NewT>

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Shorthand for when to handle only the data case.
whenOrNull<NewT>({bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, NewT? data(ValueT data)?, NewT? error(Object error, StackTrace stackTrace)?, NewT? loading()?}) → NewT?

Available on AsyncValue<ValueT>, provided by the AsyncValueExtensions extension

Perform actions conditionally based on the state of the AsyncValue.

Operators

operator ==(Object other) bool
The equality operator.
override

Static Methods

guard<ValueT>(Future<ValueT> future(), [bool test(Object)?]) Future<AsyncValue<ValueT>>
Transforms a Future that may fail into something that is safe to read.