AsyncNotifier<S>.withData constructor

AsyncNotifier<S>.withData(
  1. S data
)

Creates an AsyncNotifier with initial data.

state is the initial data to set. The notifier starts with the provided data state.

Example:

// Start with data state
AsyncNotifier<String>.withData('Initial data')

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.withData(S data) : super(AsyncState.data(data)) {
  _initialize(setState);
}