Skip to content

Invalid argument(s): Object/factory with type XXX is already registered inside GetIt. #149

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
redsuperbat opened this issue Jan 16, 2021 · 2 comments

Comments

@redsuperbat
Copy link

redsuperbat commented Jan 16, 2021

Trying to run this codeblock to register a singleton in getit. The console prints "Trying to register navigator" which indicates that the instance is not registered. However when getit the tries to register the singleton it throws.

final navigator = LandingPageNavigator(guards: model.routerGuards);
if (!GetIt.I.isRegistered(instance: LandingPageNavigator)) {
  print("Trying to register navigator");
  GetIt.I.registerSingleton<LandingPageNavigator>(navigator);
}

Stacktrace:

I/flutter ( 3418): Trying to register navigator
════════ Exception caught by widgets library ═══════════════════════════════════
The following ArgumentError was thrown building BaseWidget<LandingModel>(dirty, state: _BaseWidgetState<LandingModel>#ad5e0):
Invalid argument(s): Object/factory with  type LandingPageNavigator is already registered inside GetIt.

The relevant error-causing widget was
BaseWidget<LandingModel>
package:wine_cellar/…/landing-page/landing-page.dart:12
When the exception was thrown, this was the stack
#0      throwIf
package:get_it/get_it_impl.dart:7
#1      _GetItImplementation._register
package:get_it/get_it_impl.dart:675
#2      _GetItImplementation.registerLazySingleton
package:get_it/get_it_impl.dart:455
#3      LandingPage.build.<anonymous closure>
package:wine_cellar/…/landing-page/landing-page.dart:18
#4      _BaseWidgetState.build
package:wine_cellar/pages/base-widget.dart:43
...
════════════════════════════════════════════════════════════════════════════════
@redsuperbat redsuperbat changed the title Invalid argument(s): Object/factory with type LandingPageNavigator is already registered inside GetIt. Invalid argument(s): Object/factory with type XXX is already registered inside GetIt. Jan 16, 2021
@escamoteur
Copy link
Collaborator

This throws on the first time this part of code is executed? That should be impossible.

But your check if your singleton is registered is wrong. you are passing a type as instance. The instance parameter is only if you want to check if an existing Object is already registered.
You probably want this here:

final navigator = LandingPageNavigator(guards: model.routerGuards);
if (!GetIt.I.isRegistered<LandingPageNavigator>()) {
  print("Trying to register navigator");
  GetIt.I.registerSingleton<LandingPageNavigator>(navigator);
}

@redsuperbat
Copy link
Author

Ah geez, that was the problem. Thanks for the quick response! =)

Should have noticed i was passing a type and not the actual instance. My bad.

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