AsyncNotifier<S> constructor

AsyncNotifier<S>()

Creates an AsyncNotifier that starts in the loading state.

Use this constructor when you want the notifier to start in a loading state and then transition to data or error state based on async operations.

Example:

// Start with loading state
AsyncNotifier<String>()

Dependencies must be reachable before onInit runs.

Dart runs a superclass constructor body before the subclass's, and this constructor starts onInit. A field assigned in the subclass's own constructor body is therefore still unset inside onInit. Pass dependencies as initializing formals or assign them in the initializer list:

class UserNotifier extends AsyncNotifier<User> {
  UserNotifier(this.repository);

  final UserRepository repository;

  @override
  Future<AsyncState<User>> onInit() async =>
      AsyncState.data(await repository.current());
}

Implementation

AsyncNotifier() : super(const AsyncState.loading()) {
  _initialize(setState);
}