data_monitor 2.2.2
data_monitor: ^2.2.2 copied to clipboard
A Data Monitor package that encapsulates Annotations, Example and Utilities.
Details #
This package allows Flutter projects to utilize State Management to overcome some of the limitations enforced by the framework.
Its main use is to create data repositories or models that allow callers to be notified when changes are made to member fields. These changes are tracked internally and cause Widgets that use the data to be rebuilt.
No more calls to setState().
Features #
Can be used (with more efficiency) to rebuild StatelessWidget. You can remove
all the verbose State code that is usually associated with a StatefulWidget.
Getting started #
The package is very simple to use. Just add code like the following to create your data model class:
class ModelData extends _ModelData with _$ModelData
{
// Other methods etc for class
}
@dataMonitor
abstract class _ModelData extends DataNotifier
{
@monitor
int count = 0;
@monitor
String name = 'A Name';
}
Propagating Changes to Property Values #
When a property value changes, you can ask for its new value to be propagated.
This allows 'side-effects' or cascades to be implemented. A really simple
example can be found in the /example directory.
Note: @propagate properties @monitor their value at the same time as
providing their extra function. As such, they also respect @cache properties
(see below).
@dataMonitor
abstract class _ModelData extends DataNotifier
{
@propagate
String firstName = 'Joe';
void firstNamePropagate(String value)
{
// Do something useful with the new value,
// like setting derived properties
}
}
Caching Property Getters #
You can cache property 'values' internally to great effect, when used correctly. For example, if you had to collect data from a source that is slow or complicated, you could use the following:
@dataMonitor
abstract class _ModelData extends DataNotifier
{
@monitor
String firstName = 'Joe';
@monitor
String lastName = 'Bloggs';
@cache
Future<String> get fullName async
{
// Performs a big task asynchronously, such as getting data
// from an external database
return await humungous(firstName, lastName);
}
}
In this example, the return value from 'humungous' is cached and only ever recalculated when either 'firstName' or 'lastName' change.
Usage #
A fully working Flutter Application that describes how to use the various
features is available in the /example directory.
Additional information #
You can also use the data_monitor_generators package to automatically
generate boilerplate code from @annotations like the ones seen above
(using build_runner).