I'm using get_it for IoC. However, my app throws an exception when I try to register a bean using an async call. The function that throws the exception:
import 'package:elpee/service/localstorage_service.dart';
import 'package:get_it/get_it.dart';
GetIt locator = GetIt();
Future setupLocator() async {
LocalStorageService.getInstance().then((storageService) {
locator.registerSingleton(storageService);
});
}
The error:
Exception: Object of type LocalStorageService is not registered inside GetIt
If anyone could help me out, I'd greatly appreciate it :-)
You are trying to use get_it wrong way. Check to documentation.
First of all - get_it is Singleton, therefore don't use constructor, but
GetIt locator = GetIt.instance;
Second - implement LocalStorageService as a plain class and let the get_it to provide it as a Singleton:
void main()
...
void setupLocator() {
locator.registerLazySingleton(LocalStorageService());
}
}
To use full power of IoC define interface / abstract class definition for your storageservice and provide implementation of the interface.
void setupLocator() {
locator.registerLazySingleton<IStorageService>(LocalStorageService());
}