MutableLiveData<T> class
A mutable version of LiveData that allows changing its value.
stored value. When the value is set, all observers are automatically notified (if the value actually changed according to changeDetector).
Important: By default, change detection uses standard equality (!=).
If you modify a mutable object (like a List) in place and assign it back,
it will NOT trigger a notification because the reference is the same.
Use update for in-place modifications.
This is the most commonly used LiveData type in ViewModels for managing state that changes over time.
Example:
class CounterViewModel extends ViewModel {
final _counter = MutableLiveData(0);
LiveData<int> get counter => _counter;
void increment() {
_counter.value++; // Automatically notifies observers
}
}
See also:
- LiveData, the immutable base class
ViewModel.mutable, for creating MutableLiveData in a ViewModel scope
- Inheritance
-
- Object
- ChangeNotifier
- LiveData<
T> - MutableLiveData
- Available extensions
Constructors
- MutableLiveData(T value, [bool emitAll = false, DataScope? scope])
- Creates a MutableLiveData with an initial value.
Properties
-
changeDetector
↔ ChangeDetector<
T> -
Function that determines if the value has changed.
getter/setter pairinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
-
immutable
→ LiveData<
T> -
Returns this MutableLiveData as a LiveData reference.
no setter
- isDisposed → bool
-
no setterinherited
- isEmpty → bool
-
Available on LiveData<
Whether the current collection is empty.Iterable< , provided by the ListLiveData extensionD> >no setter - isNotEmpty → bool
-
Available on LiveData<
Whether the current collection is not empty.Iterable< , provided by the ListLiveData extensionD> >no setter - length → int
-
Available on LiveData<
The number of elements in the current collection.Iterable< , provided by the ListLiveData extensionD> >no setter - parentScope → DataScope?
-
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scope → DataScope?
-
finalinherited
-
subscribers
→ List<
dynamic Function(T)> -
no setterinherited
- value ↔ T
-
The current value held by this LiveData.
getter/setter pairoverride-getter
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
call(
) → T -
Shorthand for accessing value.
inherited
-
dispose(
) → void -
Disposes this LiveData and cleans up all resources.
inherited
-
expand<
T> (Iterable< T> toElements(D element)) → Iterable<T> -
Available on LiveData<
Expands each element into zero or more elements.Iterable< , provided by the ListLiveData extensionD> > -
filtered(
bool check(D value)) → LiveData< Iterable< D> > -
Available on LiveData<
Creates a LiveData that filters elements based on a condition.Iterable< , provided by the ListLiveData extensionD> > -
forEach(
void action(D element)) → void -
Available on LiveData<
Applies a function to each element.Iterable< , provided by the ListLiveData extensionD> > -
hotswappable(
[DataScope? scope]) → HotswapLiveData< T> -
Available on LiveData<
Converts this LiveData into a HotswapLiveData.T> , provided by the LiveDataExtensions extension -
map<
T> (T toElement(D value)) → Iterable< T> -
Available on LiveData<
Maps each element to a new value.Iterable< , provided by the ListLiveData extensionD> > -
mirror(
) → LiveData< T> -
Available on LiveData<
Creates a mirror that observes this LiveData.T> , provided by the LiveDataExtensions extension -
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.
inherited
-
notifyListeners(
) → void -
Notifies all observers and executes all subscribed callbacks.
inherited
-
notNull(
) → LiveData< Iterable< D> > -
Available on LiveData<
Creates a LiveData that filters out null elements.Iterable< , provided by the ListLiveData extensionD> > -
reload(
) → void -
Forces notification of all observers even if the value hasn't changed.
inherited
-
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.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
transform<
S> (S transform(LiveData< T> data)) → LiveData<S> -
Available on LiveData<
Transforms this LiveData into a different type.T> , provided by the LiveDataExtensions extension -
unsubscribe(
dynamic callback(T)) → void -
Removes a previously registered callback.
inherited
-
update(
dynamic block(T)) → void - Updates the value by applying a transformation function.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited