Skip to content

Object/ factory with type DocumentRepo is already registered inside GetIt #228

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

Closed
dev-horacebg opened this issue Oct 12, 2021 · 2 comments
Closed

Comments

@dev-horacebg
Copy link

dev-horacebg commented Oct 12, 2021

I'm running into an interesting issue I've not been able to get to the bottom of for some time now.
Basically, the error is telling me that I've already registered an Object/ factory inside GetIt. This seems to be the case no matter how I try to prevent registerLazySingleton() from being called again.

Default implementation

GetIt locator = GetIt.instance;

DocumentDataSource documentDataSource = NetworkDocumentDataSource();

locator.registerLazySingleton(() => DocumentRepo(documentDataSource));

Provide created instance

GetIt locator = GetIt.instance;

DocumentDataSource documentDataSource = NetworkDocumentDataSource();
DocumentRepo documentRepo = DocumentRepo(documentDataSource);

locator.registerLazySingleton(() => documentDataSource);

Check if type registered

GetIt locator = GetIt.instance;

DocumentDataSource documentDataSource = NetworkDocumentDataSource();
DocumentRepo documentRepo = DocumentRepo(documentDataSource);

if (!GetIt.I.isRegistered<DocumentRepo>()) {
    locator.registerLazySingleton(() => documentRepo);
}

Check if instance registered

GetIt locator = GetIt.instance;

DocumentDataSource documentDataSource = NetworkDocumentDataSource();
DocumentRepo documentRepo = DocumentRepo(documentDataSource);

if (!GetIt.I.isRegistered<DocumentRepo>(instance: documentRepo)) {
    locator.registerLazySingleton(() => documentRepo);
}

I can't see why I'd get this issue again with either of the last two checks. Am I missing something obvious here? Any pointers?

I know there have been a number of similarly titled issues raised in the past but I'm struggling to see what I need to do here to stop this from happening.

This happens on the latest version:7.2.0 and other historic versions we've used in the past.

@dev-horacebg
Copy link
Author

Querying the locator instance I created directly did the job 😓

if (!locator.isRegistered<DocumentRepo>()) {
    locator.registerLazySingleton(() => documentRepo);
}

@escamoteur
Copy link
Collaborator

That is pretty weird. So you say if you use locator it worked but if you used GetIt.I it didn't?

could you give me a simple repro project that shows the effect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants