This package helps you to manage any dependencies in your project without need to use context and makes possible to create dependencies for modules. The objective is help any developer to manage dependencies with a easy to use API.
It's basically the copy of this package Flutter Injections.
- Fast and Efficient
Flutter Injections use search-tree to get the dependencies, this improve the speed to get them, and use less CPU to search for specific objects.
- Module Injections
Create module injections that have all dependencies needed on your widgets. I.E
HomeModule
have all dependencies needed onHomePage
. - Easy to use
The focus is to keep it simple to handle dependencies on large scale applications.
- Auto dispose
Objects are auto disposed when the
module
is removed from the Widget Tree.
It`s simple, just three steps:
-
Add the TreeMan to pubspec.yaml file
tree_man: ^any # or current version
-
Create a Module
class MainClass { final String name = 'name'; } class MainModule extends Module { @override List<Inject<Object>> get injections => [ Inject<MainClass>.singleton( (_) => MainClass(), ), ]; }
-
Create your DepsModule and pass the
module
MaterialApp( home: FlutterModule( createModule: MainModule.new, builder: (_) => const Text('Teste'), loading: const Text('loading'), ), ),
-
And finally, use it to get the dependencies:
final controller = Deps.get<YourController>();
You can use the Inject.asyncSingleton
to create async injections.
For know if the module is ready you can use the Deps.isModuleReady
method and for waiting the async module to be ready you can use the Deps.waitAsyncModuleIsReady
method.
The FlutterModule
widget is already in control of this for default and has a loading
parameter that is used when the module is not ready, if null then the CircularProgressIndicator
is used.
MaterialApp(
home: FlutterModule(
createModule: AsyncMainModule.new,
builder: (_) => const Text('Teste'),
loading: const Text('loading'),
),
),
When you use the FlutterModule
widget, the dependencies of the module are disposed of when the widget is removed from the tree and the dispose method of Injection
is called.
Inject<DisposeClass>.singleton(
(_) => DisposeClass(),
dispose: (instance) => instance.dispose(),
),
Called the Deps.addModule
for added a module e for remove you must call Deps.removeModule
that will dispose all injections in the module automatically.
The FlutterModule
widget calls these methods by default.
You can override the dependencies using the overrideInstance
method.
Deps.overrideInstance<YourRepository>(YourRepositoryMock());
then
Deps.get<YourRepository>() will return `YourRepositoryMock`.
You liked this package? Then give it a ⭐️. If you want to help then:
- Fork this repository
- Send a Pull Request with new features
- Share this package
- Create issues if you find a bug or want to suggest a new extension
Pull Request title follows Conventional Commits.
Copyright © 2024 Kauê Martins.
This project is MIT licensed.