transformDataState method
- T? transformer(
- T? data
Transforms the data within the current success state using the
provided transformer function and notifies listeners.
If the current state is not a success state, or if the current data is null
and the transformer cannot handle null, the behavior might lead to
unexpected states or errors depending on the transformer's implementation.
It's generally expected that this method is called when _state.data is valid
or the transformer can gracefully handle null.
The transformer function receives the current data T? (which might be null
if the state was AsyncState.success(null) or if T is nullable)
and should return the new data T. The state will then be updated to
AsyncState.success with this new data.
After the transformation, listeners are notified.
Example:
// Assuming T is List<String> and _state is AsyncState.success(["apple"])
// Add an item to the list
transformDataState((currentData) {
return [...?currentData, "banana"];
});
// _state is now AsyncState.success(["apple", "banana"]) and listeners are notified.
// If T is int and _state is AsyncState.success(5)
transformDataState((currentData) => (currentData ?? 0) + 1);
// _state is now AsyncState.success(6)
- Parameter transformer: A function that takes the current data
T?from a success state and returns the new dataT.
Implementation
void transformDataState(T? Function(T? data) transformer) {
final transformData = transformer(_state.data);
if (transformData != null) {
updateState(transformData);
} else {
assert(() {
if (!ReactiveNotifier.debugLogging) return true;
log(
'⚠️ transformDataState<${T.toString()}> returned null - transformation ignored',
);
return true;
}());
}
}