DataScope class

Manages the lifecycle of ChangeNotifier instances, particularly LiveData.

DataScope provides automatic disposal of registered notifiers when the scope is disposed. It supports hierarchical scopes through parent-child relationships, ensuring proper cleanup of resources.

Commonly used within ViewModel to manage all LiveData instances, but can also be used standalone for managing any ChangeNotifier lifecycle.

Example:

final scope = DataScope();
final data1 = scope.add(MutableLiveData(0));
final data2 = scope.mutable('hello');

// Later, dispose all at once
scope.dispose(); // data1 and data2 are automatically disposed

See also:

  • ViewModel, which uses DataScope internally
  • LiveData, the primary type managed by DataScope
Available extensions

Constructors

DataScope({DataScope? parent})
Creates a new DataScope, optionally with a parent scope.

Properties

children → LinkedHashSet<DataScope>
no setter
hashCode → int
The hash code for this object.
no setterinherited
items → LinkedHashSet<ChangeNotifier>
no setter
parent → DataScope?
The parent scope, if this is a child scope.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

add<T extends ChangeNotifier>(T data) → T
Adds a ChangeNotifier to this scope and returns it.
bridgeFrom<T>(LiveData<T> source) → MutableLiveData<T>

Available on DataScope, provided by the MutableDataScope extension

Creates a MutableLiveData that mirrors a source LiveData.
child() → DataScope
Creates a child scope with this scope as its parent.
clean(ChangeNotifier data) → void
Removes and disposes a ChangeNotifier from this scope.
cleanScope() → void
Cleans up all child scopes and items in this scope.
dispose() → void
Disposes this scope and all registered items and child scopes.
join<T>(List<LiveData> sources, T mediate()) → LiveData<T>

Available on DataScope, provided by the DataScopeExtensions extension

Combines multiple LiveData sources using a mediator function.
merge<T>(List<ChangeNotifier> sources, T transform()) → LiveData<T>

Available on DataScope, provided by the DataScopeExtensions extension

Merges multiple ChangeNotifier sources into a single LiveData.
mutable<T>(T start) → MutableLiveData<T>

Available on DataScope, provided by the MutableDataScope extension

Creates and registers a MutableLiveData with an initial value.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remove(ChangeNotifier data) → bool
Removes a ChangeNotifier from this scope without disposing it.
toString() → String
A string representation of this object.
inherited

Operators

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