final locator =GetIt.instance;Future<void> init() async { locator.registerSingleton<T>(T instance);/// registers a type as Singleton by passing an [instance] of that type /// that will be returned on each call of [get] on that type
final locator =GetIt.instance;Future<void> init() async { locator.registerLazySingleton<T>(FactoryFunc<T> func);/// registers a type as Singleton by passing a factory function that will be called /// on the first call of [get] on that type
๊ทธ๋ฆฌ๊ณ usecase, viewModel ์ di ์ฒ๋ฆฌ์ application layer ์์์ di ์ฒ๋ฆฌ ์ฌ์ด์ ๊ฐ์ฅ ํฐ ์ฐจ์ด๋ผ ํ๋ค๋ฉด registerFactory() ๋ฅผ ์ฌ์ฉํ๋ค๋ ๊ฒ์ด๋ค.
registers a type so that a new instance will be created on each call of [get] on that type ์ค๋ช ๊ณผ ๊ฐ์ด ์ฌ์ฉ ๋ ๋๋ง๋ค ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ฐํํด์ฃผ๋๋ก ํ๊ธฐ ์ํด์ Factory ๋ฅผ ๋ฑ๋กํ๋ค.
static Route<dynamic> getRoute(RouteSettings routeSettings) {
switch (routeSettings.name) {
case Routes.splashRoute:
return MaterialPageRoute(builder: (_) => SplashView());
case Routes.loginRoute:
initLoginModule();
return MaterialPageRoute(builder: (_) => LoginView());
case Routes.onBoardingRoute:
return MaterialPageRoute(builder: (_) => OnBoardingView());
case Routes.registerRoute:
initRegisterModule();
return MaterialPageRoute(builder: (_) => RegisterView());
case Routes.forgotPasswordRoute:
initForgotPasswordModule();
return MaterialPageRoute(builder: (_) => ForgotPasswordView());
case Routes.mainRoute:
initHomeModule();
return MaterialPageRoute(builder: (_) => MainView());
case Routes.storeDetailsRoute:
initStoreDetailsModule();
return MaterialPageRoute(builder: (_) => StoreDetailsView());
default:
return unDefinedRoute();
}
}
/// registers a type so that a new instance will be created on each call of [get] on that type
/// [T] type to register
/// [factoryFunc] factory function for this type
/// [instanceName] if you provide a value here your factory gets registered with that
/// name instead of a type. This should only be necessary if you need to register more
/// than one instance of one type. Its highly not recommended
void registerFactory<T extends Object>(
FactoryFunc<T> factoryFunc, {
String? instanceName,
});