LiveData<T> class abstract

An observable data holder for the MVVM pattern.

LiveData is the core class of the mvvm_kit package. It holds a value and notifies observers when that value changes. Use it with Watch or GroupWatch widgets to automatically rebuild UI when data changes.

This is an abstract class. Use MutableLiveData for data you can modify, or create instances using factory methods like LiveData.fromValueNotifier and LiveData.fromStream.

Example:

class CounterViewModel extends ViewModel {
  final _counter = MutableLiveData(0);
  LiveData<int> get counter => _counter;

  void increment() => _counter.value++;
}

// In your widget:
Watch(
  viewModel.counter,
  builder: (context, value) => Text('$value'),
)

See also:

  • MutableLiveData, for creating mutable observable data
  • Watch, for observing a single LiveData in widgets
  • GroupWatch, for observing multiple LiveData objects
Inheritance
Implementers
Available extensions

Constructors

LiveData([T? value, DataScope? parentScope])
LiveData.fromValueNotifier(ValueNotifier<T> notifier, [DataScope? scope])
Converts a ValueNotifier into a LiveData.
factory

Properties

changeDetector ↔ ChangeDetector<T>
Function that determines if the value has changed.
getter/setter pair
hashCode → int
The hash code for this object.
no setterinherited
hasListeners → bool
Whether any listeners are currently registered.
no setterinherited
isDisposed → bool
no setter
isEmpty → bool

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

Whether the current collection is empty.
no setter
isNotEmpty → bool

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

Whether the current collection is not empty.
no setter
length → int

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

The number of elements in the current collection.
no setter
parentScope → DataScope?
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
scope → DataScope?
final
subscribers → List<dynamic Function(T)>
no setter
value → T
The current value held by this LiveData.
no setter

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
call() → T
Shorthand for accessing value.
dispose() → void
Disposes this LiveData and cleans up all resources.
override
expand<T>(Iterable<T> toElements(D element)) → Iterable<T>

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

Expands each element into zero or more elements.
filtered(bool check(D value)) → LiveData<Iterable<D>>

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

Creates a LiveData that filters elements based on a condition.
forEach(void action(D element)) → void

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

Applies a function to each element.
hotswappable([DataScope? scope]) → HotswapLiveData<T>

Available on LiveData<T>, provided by the LiveDataExtensions extension

Converts this LiveData into a HotswapLiveData.
map<T>(T toElement(D value)) → Iterable<T>

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

Maps each element to a new value.
mirror() → LiveData<T>

Available on LiveData<T>, provided by the LiveDataExtensions extension

Creates a mirror that observes this LiveData.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyIfChanged() → void
Notifies all observers only if the value has changed.
notifyListeners() → void
Notifies all observers and executes all subscribed callbacks.
override
notNull() → LiveData<Iterable<D>>

Available on LiveData<Iterable<D>>, provided by the ListLiveData extension

Creates a LiveData that filters out null elements.
reload() → void
Forces notification of all observers even if the value hasn't changed.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
subscribe(dynamic callback(T)) → LiveData<T>
Subscribes to value changes with a callback function.
toString() → String
A string representation of this object.
inherited
transform<S>(S transform(LiveData<T> data)) → LiveData<S>

Available on LiveData<T>, provided by the LiveDataExtensions extension

Transforms this LiveData into a different type.
unsubscribe(dynamic callback(T)) → void
Removes a previously registered callback.

Operators

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

Static Methods

fromStream<T>(Stream<T> stream, T initialValue, [DataScope? scope]) → LiveData<T?>
Creates a nullable LiveData from a Stream.