WayFar
Search…
Dependency Injection

Service locator

To be able to register dependencies and use them widely in the app, we use the get_it package which is a service locator for Flutter projects.
It offers many possibilities to register objects: singletons, factories,..., see the example below:
1
sl.registerLazySingleton(()=> Storage());
2
sl.registerLazySingleton(() => ThemeBloc());
Copied!
Note that sl is a global variable, located in the file lib/src/core/di/locator.dart

Configuring features

When you add a new feature you can use register the corresponding dependencies by using the class Feature :
After overriding required methods and registering the necessary objects, head over to main.dart under the folder lib/src/core and configure the feature by calling the init() method :
main.dart
1
/// Initialize the app, by initializing all the app features
2
///
3
init() {
4
_registerCoreDependencies();
5
AuthFeature.init();
6
...
7
BookmarksFeature.init();
8
HomeFeature.init();
9
// Add new feature here
10
}
Copied!
The order of instructions is important in the init function since we can have interdependent features.