Popsiclev2.1.0
GitHub ↗
Popsicle docs · 2.1.0

Async State

Represent loading, data, refresh, and errors with composable AsyncState.

Async State

Popsicle keeps async state as a normal immutable value:

Store<AsyncState<User>>

No separate async Store hierarchy is required.

class UserStore extends Store<AsyncState<User>> {
  UserStore() : super(const AsyncState.idle());

  Future<void> load() async {
    final previous = state;
    commit(AsyncState.loading(previous: previous));

    try {
      final user = await fetchUser();
      commit(AsyncState.data(user));
    } catch (error, stackTrace) {
      commit(
        AsyncState.error(
          error,
          stackTrace,
          previous: previous,
        ),
      );
    }
  }
}

For multiple independent sources:

final combined = Async.combine2(user, posts);

or:

final combined = user.zip(posts);

AsyncState can preserve stale values during refresh and refresh errors, allowing UI to keep rendering useful content.