notifyListeners method

  1. @override
void notifyListeners()
override

Notify dependents that this provider has changed.

This is typically used for mutable state, such as to do:

class TodoList extends Notifier<List<Todo>> {
  @override
  List<Todo>> build() => [];

  void addTodo(Todo todo) {
    state.add(todo);
    ref.notifyListeners();
  }
}

Implementation

@override
void notifyListeners() {
  final currentResult = getState();
  // If `notifyListeners` is used during `build`, the result will be null.
  // Throwing would be unnecessarily inconvenient, so we simply skip it.
  if (currentResult == null) return;

  if (_didBuild) {
    _notifyListeners(
      currentResult,
      currentResult,
      checkUpdateShouldNotify: false,
    );
  }
}