Service<T> class abstract

An abstract class that represents the ViewModel in the MVVM architecture pattern. Primarily responsible for data loading and state management.

Inheritance
Available extensions

Constructors

Service({ServiceStatus initialStatus = ServiceStatus.none})

Properties

canDebugPrint bool
Determines whether this service should log errors using debugPrint. Subclasses can override to disable or customize logging.
no setter
canRethrow bool
Determines whether this service should rethrow errors from load. Can be overridden by subclasses to customize behavior.
no setter
data ↔ T
Returns the currently deserialized and loaded data.
getter/setter pair
error → dynamic
Returns the current error. Throws if no error is present.
no setter
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
isError bool
Returns true if the service encountered an error while fetching data.
no setter
isLoading bool
Returns true if the data is currently being loaded or not ready for display. Typically used to show loading indicators or placeholders.
no setter
isRefreshing bool
Returns true if the service is currently refreshing its data. Useful for pull-to-refresh indicators or temporarily disabling UI interactions.
no setter
maybeData → T?
Returns the current data regardless of its existence.
no setter
maybeError → dynamic
Returns the current error regardless of its existence.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
status ServiceStatus
Returns the current status of the Service.
no setter

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
done(T newData) → void
Called when data has been successfully loaded.
fail(dynamic error) → void
Called when a data load fails.
fetchData() Future<T>
Fetches the data for this service.
load({bool isRefresh = false}) Future<void>
Starts loading or refreshing the data managed by this service.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
notifyUpdated() → void
Notifies listeners that the internal data has been updated.
refresh() Future<void>
Requests a refresh while keeping existing data until new data is loaded.
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
when({Widget none()?, Widget loading()?, Widget refresh(T data)?, Widget loaded(T data)?, Widget failed(Object error)?}) Widget

Available on Service<T>, provided by the ServiceWhen extension

Returns a widget depending on the current ServiceStatus.

Operators

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

Static Methods

maybeOf<T extends Service>(BuildContext context) → T?
Finds the Service from the closest instance of this class that encloses the given context.
of<T extends Service>(BuildContext context) → T
Finds the Service from the closest instance of this class that encloses the given context.