Dependency Injection In Multi-Module Flutter Project

Photo by bruce mars on Unsplash

Using “get_it” & “injectable” package to configure dependencies in a multi-module project.

Feature Module:

// simple class from feature_module@Singleton()
Class Engine{
}
===========================// configure injectable in feature module@InjectableInit(
initializerName: r'$initModuleGetIt'
)
void configureModuleDependencies(GetIt getIt) => $initModuleGetIt(getIt);
// class from main module@Singleton()
class Vehicle {
final Engine engine;
Vehicle({this.engine});
}
========================// Configure injectable in main modulefinal getIt = GetIt.instance;
@InjectableInit(
initializerName: r'$initGetIt'
)
void configureDependencies() {
// configureDataDependencies imported from feature_module
configureDataDependencies(getIt);
$initGetIt(getIt);
}
void main() {
configureDependencies();
runApp(MyApp());
}
Vehicle vehicle = getIt.get();
OR
var vehicle = getIt.get<Vehicle>();
Photo by Ambreen Hasan on Unsplash

Did I get something wrong? Mention it in the comments. I would love to improve. 😊

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store