ComputedValueNotifier<T> class

A class that can be used to derive a value based on data from another Listenable or Listenables.

The value will be recomputed when the provided _listenable notifies the listeners that values have changed.

Simple Example

final email = ValueNotifier<String>('a');

// Determine whether or not the email is valid using a (hacky) validator.
final emailValid = ComputedValueNotifier(
  email,
  () => email.value.contains('@'),
);

// The function provided to ComputedValueNotifier is immediately executed,
// and the computed value is available synchronously.
print(emailValid.value); // prints 'false'.

// When the email ValueNotifier is changed, the function will be run again!
email.value = 'a@b.com';
print(emailValid.value); // prints 'true'.

Deriving data from multiple listenables

In this case, we can use the Lisetenable.merge function provided by Flutter to merge several variables.

final email = ValueNotifier<String>('');
final password = ValueNotifier<String>('');

// Determine whether the email is valid, and make that a Listenable!
final emailValid = ComputedValueNotifier<bool>(
  email,
  () => email.value.contains('@'),
);

// Determine whether the password is valid, and make that a Listenable!
final passwordValid = ComputedValueNotifier<bool>(
  password,
  () => password.value.length >= 6,
);

// Now, we will only enable the "Login Button" when the email and
// password are valid. To do so, we can listen to the emailValid and
// passwordValid ComputedValueNotifiers.
final loginButtonEnabled = ComputedValueNotifier<bool>(
  Listenable.merge([emailValid, passwordValid]),
  () => emailValid.value && passwordValid.value,
);

// Update the email
print(emailValid.value); // false
print(loginButtonEnabled.value); // false
email.value = 'a@b.com';
print(emailValid.value); // true
print(loginButtonEnabled.value); // false

// Update the password
print(passwordValid.value); // false
password.value = '123456';
print(passwordValid.value); // true
print(loginButtonEnabled.value); // true
Inheritance
Implemented types

Constructors

ComputedValueNotifier({required Listenable listenable, required T compute()})
Computed value based on another listenable

Properties

hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value → T
The current value stored in this notifier.
no setteroverride

Methods

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

Operators

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