registrar 0.3.0 registrar: ^0.3.0 copied to clipboard
A Flutter hybrid locator that locates both single services (similar to GetIt) and inherited models (similar to Provider, InheritedWidget). Supports migrating inherited models to single services.
registrar #
A Flutter hybrid locator that locates both single services (similar to GetIt) and inherited models (similar to Provider, InheritedWidget). Supports migrating inherited models to single services.
Registrar goals:
- Locate single services from anywhere.
- Locate inherited models in the widget tree.
- Support lazy loading.
- Support migrating an inherited model to a single service.
- Work alone or with other state management packages (RxDart, Provider, GetIt, ...).
- Be scalable and performant, so suitable for both indy and production apps.
Single Services #
Single services are those services where you only need one of them and need to locate them from anywhere in the widget tree.
To add a single service to the registry, give a builder to a "Registrar" widget and add it to the widget tree:
Registrar<MyModel>(
builder: () => MyModel(),
child: MyWidget(),
);
Inherited Models #
Inherited models are located on the widget tree (similar to Provider, InheritedWidget). Unlike single services, you can add as many inherited models as you need.
Adding inherited models to the widget tree uses the same Registrar widget, but with the inherited
parameter:
Registrar<MyModel>(
builder: () => MyModel(),
inherited: true,
child: MyWidget(),
);
Registrar widgets unregister their services and models when they are removed from the widget tree. If their services and models are ChangeNotifiers, the Registrar widgets optionally call the ChangeNotifiers' dispose
method.
How to Locate Single Services #
The single service instance can located from anywhere by type:
final myModel = Registrar.get<MyModel>();
If more than one instance of a model of the same type is needed, you can specify a unique name:
Registrar<MyModel>(
builder: () => MyModel(),
name: 'some unique name',
child: MyWidget(),
);
And then get the model by type and name:
final myModel = Registrar.get<MyModel>(name: 'some unique name');
Unlimited Registrar widgets can be added to the widget tree. If you want to manage multiple models with a single widget, use MultiRegistrar:
MultiRegistrar(
delegates: [
RegistrarDelegate<MyModel>(builder: () => MyModel()),
RegistrarDelegate<MyOtherModel>(builder: () => MyOtherModel()),
],
child: MyWidget(),
);
For rare use cases where you need to directly manage registering and unregistering models (instead of letting Registrar and MultiRegistrar manage your models), you can use the static register
and unregister
functions:
Registrar.register<MyModel>(builder: () => MyModel(''))
How to Located Inherited Models #
Registrar implements the observer pattern as a mixin that can my added to your models and widgets.
class MyModel with Observer {
int counter;
}
Models and widgets that use Observer can listenTo
registered single services:
final text = listenTo<MyWidgetViewModel>(listener: myListener).text;
And listenTo
inherited models on the widget tree. (Just add the context
parameter to search the widget tree instead of the registry):
final text = listenTo<MyWidgetViewModel>(context: context, listener: myListener).text;
For convenience, Observer also adds a get
function that doesn't required the preceding Registrar class name. Models and widgets that use Observer can get registered single services:
final text = get<MyModel>().text;
And get inherited models:
final text = get<MyModel>(context: context).text;
Example #
(The source code for this example is under the Pub.dev "Example" tab and in the GitHub example/lib/main.dart
file.)
There are three registered services:
- ColorNotifier changes its color every N seconds and then calls
notifyListeners
. - FortyTwoService holds a number that is equal to 42.
- RandomService generates a random number.
The first service was added to the widget tree with Registrar
. The remaining services were added with MultiRegistrar
.
That's it! #
If you have questions or suggestions on anything Registrar, please do not hesitate to contact me.