Skip to content

kmartins/tree_man

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tree man

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.

Why Tree Man?

  • 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 on HomePage.

  • 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.

How to use

It`s simple, just three steps:

  1. Add the TreeMan to pubspec.yaml file

    tree_man: ^any # or current version
  2. Create a Module

     class MainClass {
         final String name = 'name';
     }
    
     class MainModule extends Module {
         @override
         List<Inject<Object>> get injections => [
             Inject<MainClass>.singleton(
             (_) => MainClass(),
             ),
         ];
     }
  3. Create your DepsModule and pass the module

    MaterialApp(
        home: FlutterModule(
            createModule: MainModule.new,
            builder: (_) => const Text('Teste'),
            loading: const Text('loading'),
        ),
    ),
  4. And finally, use it to get the dependencies:

final controller = Deps.get<YourController>();

Async Injections

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'),
    ),
),

Dispose

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(),
),

Module

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.

Test

You can override the dependencies using the overrideInstance method.

Deps.overrideInstance<YourRepository>(YourRepositoryMock());

then

Deps.get<YourRepository>() will return `YourRepositoryMock`.

📝 Maintainers

Kauê Martins

🤝 Support

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.

📝 License

Copyright © 2024 Kauê Martins.
This project is MIT licensed.

About

Package for dependency injection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages