ChangeNotifier class mixin

A class that can be extended or mixed in that provides a change notification API using VoidCallback for notifications.

It is O(1) for adding listeners and O(N) for removing listeners and dispatching notifications (where N is the number of listeners).

Using ChangeNotifier subclasses for data models

A data structure can extend or mix in ChangeNotifier to implement the Listenable interface.

The following example implements a simple counter whose current count is stored in a ChangeNotifier subclass, notifying clients when the value changes:

// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: avoid_print

import 'package:listen/listen.dart';

/// A simple counter that extends [ChangeNotifier] to notify listeners
/// whenever its value changes.
class Counter extends ChangeNotifier {
  int _count = 0;

  /// The current count value.
  int get count => _count;

  /// Increments the count by one and notifies listeners.
  void increment() {
    _count++;
    notifyListeners();
  }
}

void main() {
  final counter = Counter();

  counter.addListener(() {
    print('Counter value changed to: ${counter.count}');
  });

  counter.increment(); // Prints: Counter value changed to: 1
  counter.increment(); // Prints: Counter value changed to: 2

  counter.dispose();
}

In this case, the ChangeNotifier subclass encapsulates a list, notifying clients whenever an item is added or removed:

// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: avoid_print

import 'package:listen/listen.dart';

/// A [ChangeNotifier] subclass that encapsulates a list of items and notifies
/// listeners whenever items are added or removed.
class ItemListNotifier extends ChangeNotifier {
  final List<String> _items = <String>[];

  /// The unmodifiable list of current items.
  List<String> get items => List<String>.unmodifiable(_items);

  /// Adds an [item] to the list and notifies listeners.
  void addItem(String item) {
    _items.add(item);
    notifyListeners();
  }

  /// Removes an [item] from the list and notifies listeners if it was present.
  void removeItem(String item) {
    if (_items.remove(item)) {
      notifyListeners();
    }
  }
}

void main() {
  final listNotifier = ItemListNotifier();

  listNotifier.addListener(() {
    print('Current items: ${listNotifier.items}');
  });

  listNotifier.addItem('Apple'); // Prints: Current items: [Apple]
  listNotifier.addItem('Banana'); // Prints: Current items: [Apple, Banana]
  listNotifier.removeItem('Apple'); // Prints: Current items: [Banana]

  listNotifier.dispose();
}

See also:

Implemented types
Implementers

Constructors

ChangeNotifier()

Properties

hashCode → int
The hash code for this object.
no setterinherited
hasListeners → bool
Whether any listeners are currently registered.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
override
dispose() → void
Discards any resources used by the object.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

debugAssertNotDisposed(ChangeNotifier notifier) → bool
Used by subclasses to assert that the ChangeNotifier has not yet been disposed.
maybeDispatchObjectCreation(ChangeNotifier object) → void
Dispatches the event of the object creation to Listenable.debugMaybeDispatchCreated.