RemoteState<T> class Null safety

A sealed class representing fetched data and the states it can be in.

If you find yourself continually using enums and classes to represent loaded data, or you have a habit of shuffling errors away to where they can be quietly ignored, consider using RemoteState. It makes it easier to represent the real state of a remote data fetch and handle it properly.

See also:

How to fix a bad user interface

Annotations
  • @freezed

Constructors

RemoteState.error([Object? error, StackTrace? stackTrace])
We asked, but something went wrong. Here's the error.
factory
RemoteState.initial()
We haven't asked for the data yet.
factory
RemoteState.loading()
We've asked, but haven't got an answer yet.
factory
RemoteState.success(T value)
Everything worked, and here's the data.
factory

Properties

hashCode int
The hash code for this object.
read-onlyinherited
isError bool
State-checking predicate. Returns true if we've failed to load some data. Returns true only if RemoteState is RemoteState.error
read / write
isInitial bool
State-checking predicate. Returns true if we haven't asked for data yet. Returns true only if RemoteState is RemoteState.initial
read / write
isLoading bool
State-checking predicate. Returns true if we're loading. Returns true only if RemoteState is RemoteState.loading
read / write
isSuccess bool
State-checking predicate. Returns true if we've successfully loaded some data. Returns true only if RemoteState is RemoteState.success
read / write
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited

Methods

map<TResult extends Object?>({required TResult initial(_Initial<T> value), required TResult loading(_Loading<T> value), required TResult success(_Success<T> value), required TResult error(_Error<T> value)}) → TResult
Achieves the same result as when without destructuring.
override
maybeMap<TResult extends Object?>({TResult initial(_Initial<T> value)?, TResult loading(_Loading<T> value)?, TResult success(_Success<T> value)?, TResult error(_Error<T> value)?, required TResult orElse()}) → TResult
Achieves the same result as maybeWhen without destructuring.
override
maybeWhen<TResult extends Object?>({TResult initial()?, TResult loading()?, TResult success(T value)?, TResult error(Object? error, StackTrace? stackTrace)?, required TResult orElse()}) → TResult
Defines logic based on the current state with a fallback for unhandled states.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
when<TResult extends Object?>({required TResult initial(), required TResult loading(), required TResult success(T value), required TResult error(Object? error, StackTrace? stackTrace)}) → TResult
Defines logic based on the current state.
override

Operators

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

Static Methods

guard<T>(Future<T> future()) Future<RemoteState<T>>
Convert a Future to RemoteState. Emits RemoteState.success if the future completes Emits RemoteState.error if future fails.