onInit method

void onInit()
inherited

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

Override this method to perform setup logic, such as initializing resources or starting listeners.

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 MyNotifier extends Notifier<int> with NotifierLifecycle<int> {
  MyNotifier() : super(0);

  @override
  void onInit() {
    // Initialize resources, start listeners, etc.
    print('Notifier initialized');
  }
}

Implementation

void onInit() {}