Model class

A base class that holds some data and allows other classes to listen to changes to that data.

In order to notify listeners that the data has changed, you must explicitly call the notifyListeners method.

Generally used in conjunction with a ScopedModel Widget, but if you do not need to pass the Widget down the tree, you can use a simple AnimatedBuilder to listen for changes and rebuild when the model notifies the listeners.

Example

class CounterModel extends Model {
  int _counter = 0;

  int get counter => _counter;

  void increment() {
    // First, increment the counter
    _counter++;

    // Then notify all the listeners.
    notifyListeners();
  }
}
Inheritance

Constructors

Model()

Properties

listenerCount int
Returns the number of listeners listening to this model.
read-only
hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

addListener(VoidCallback listener) → void
listener will be invoked when the model changes.
override
notifyListeners() → void
Should be called only by Model when the model has changed.
@protected
removeListener(VoidCallback listener) → void
listener will no longer be invoked when the model changes.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited