-
-
Notifications
You must be signed in to change notification settings - Fork 153
registerLazySingleton unexpected behavior #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Which version are you using? |
get_it: 3.1.0 |
This is definitively strange. I have a closer look |
It works for me see my Test Code: test('register LazySingleton with lambda and factory function', () {
GetIt.I.registerLazySingleton(() => SingletonInjector.configuration());
final instance = GetIt.I<Injector>();
expect(instance, TypeMatcher<Injector>());
});
}
class SingletonInjector {
static Injector configuration() {
return Injector();
}
}
class Injector {} I would have to see the code for your injector and SinglettonInjector. Overall I want to add, you should always provide a type or a name when registering something, you didn't either. it only worked because of type inference. |
Please also test the latest beta #46 |
//########################################################################
// injector_interface.dart
----------------------------
enum RepoType {
MOCK,
PROD,
}
abstract class Injector{
//getter and setter
LocationTracker get locationTracker;
Repository get repo;
//method
void configureInjector(RepoType repoType);
void dispose();
}
//#######################################################################
//singleton_injector.dart
---------------------------
class SingletonInjector implements Injector {
//class property
static final SingletonInjector _singleton = SingletonInjector._internal();
static RepoType _repoType;
//getter and setter
//class constructor
factory SingletonInjector.configuration(RepoType repoType) {
_configure(repoType);
return _singleton;
}
SingletonInjector._internal();
//class methods
static void _initRepo() {
//repository
switch (_repoType) {
case RepoType.MOCK:
GetIt.I.registerLazySingleton<Repository>(() => RepoPROD());
break;
default:
GetIt.I.registerLazySingleton<Repository>(() => RepoPROD());
}
//location tracker
}
//configure injector methods
@override
void configureInjector(RepoType repoType) {
_configure(repoType);
}
//config wrapper
static void _configure(RepoType repoType) {
print("_configure from injector");
GetIt.I.reset();
_repoType = repoType;
_initRepo();
kIsWeb ? _configureForWeb() : _configureForMobile();
}
@override
void dispose() {
repo.cloudDB.dispose();
GetIt.I.reset();
}
} |
this is the code for injector and its singleton implementation |
Yeah, I guess Dart has a problem infferring the type for factory constructors please do `` Dart
|
thank you for your reply |
This does NOT WORK,
GetIt.I.registerLazySingleton(() => SingletonInjector.configuration(RepoType.PROD));
ERROR,
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following ArgumentError was thrown building Builder(dirty):
Invalid argument (Object of type Injector is not registered inside GetIt.
Did you forget to pass an instance name?
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;)): Instance
of '_Type'
But this WORKS,
Injector inj = SingletonInjector.configuration(RepoType.PROD);
GetIt.I.registerLazySingleton(() => inj);
The text was updated successfully, but these errors were encountered: