onInit abstract method

Future<AsyncState<S>> onInit()
inherited

Called when the notifier is first initialized and attached to the widget tree.

Override this method to perform asynchronous setup, such as fetching initial data, starting listeners, or other async initialization tasks. The returned AsyncState is typically used to update the notifier's state.

This method is automatically invoked after the notifier is created and attached to the widget tree. Do not call onInit manually from builder widget onInit callbacks, as this will result in duplicate initialization.

Example (incorrect usage):

NotifierBuilder<Notifier, State>(
  // Do NOT do this:
  onInit: (notifier) => notifier.onInit(),
  builder: (context, state) => Text('State: $state'),
)

Example (correct usage):

class MyAsyncNotifier extends AsyncNotifier<String> with AsyncNotifierLifecycle<String> {
  MyAsyncNotifier() : super();

  @override
  Future<AsyncState<String>> onInit() async {
    // Fetch initial data
    final data = await _fetchData();
    return AsyncState.data(data);
  }
}

Returns a Future that completes with the new AsyncState for the notifier.

Implementation

Future<AsyncState<S>> onInit();