Skip to content

Long time issue with Flutter web hot reload: "Object/factory with type is not registered inside GetIt." #235

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
stx opened this issue Dec 11, 2021 · 3 comments
Labels

Comments

@stx
Copy link

stx commented Dec 11, 2021

First, thanks for this incredible package. I've been using it in many different projects and it makes developing on Flutter even more delightful.

Second, I've been dealing with this issue for about a year across multiple projects: whenever hot reloading on the web, I'll frequently but randomly encounter the following:

"Object/factory with  type UserService is not registered inside GetIt. \n(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;\nDid you forget to register it?)"
    at Object.throw_ [as throw] (http://localhost:1234/dart_sdk.js:5061:11)
    at Object.assertFailed (http://localhost:1234/dart_sdk.js:4986:15)
at get_it._GetItImplementation.new.[_findFactoryByNameAndType] (http://localhost:1234/packages/get_it/get_it.dart.lib.js:664:44)
at get_it._GetItImplementation.new.get (http://localhost:1234/packages/get_it/get_it.dart.lib.js:672:60)
at get_it._GetItImplementation.new.call (http://localhost:1234/packages/get_it/get_it.dart.lib.js:688:19)
    at _RootZone.runUnaryGuarded (http://localhost:1234/dart_sdk.js:38448:11)
    at _ControllerSubscription.new.[_sendData] (http://localhost:1234/dart_sdk.js:32328:22)
    at _ControllerSubscription.new.[_add] (http://localhost:1234/dart_sdk.js:32277:26)
    at _MultiStreamController.new.addSync (http://localhost:1234/dart_sdk.js:36275:56)
at _MultiControllerSink.new.add (http://localhost:1234/packages/rxdart/src/utils/forwarding_stream.dart.lib.js:29:32)
at _StartWithStreamSink.new.onData (http://localhost:1234/packages/rxdart/src/transformers/start_with.dart.lib.js:21:26)
    at _RootZone.runUnaryGuarded (http://localhost:1234/dart_sdk.js:38448:11)
    at _BroadcastSubscription.new.[_sendData] (http://localhost:1234/dart_sdk.js:32328:22)
    at _DelayedData.new.perform (http://localhost:1234/dart_sdk.js:35633:28)
    at _StreamImplEvents.new.handleNext (http://localhost:1234/dart_sdk.js:35737:15)
    at async._AsyncCallbackEntry.new.callback (http://localhost:1234/dart_sdk.js:35498:16)
    at Object._microtaskLoop (http://localhost:1234/dart_sdk.js:38778:13)
    at _startMicrotaskLoop (http://localhost:1234/dart_sdk.js:38784:13)
    at http://localhost:1234/dart_sdk.js:34519:9

I never encounter this otherwise, but it makes developing on the web with get_it more painful than it should be. Hot restarting/refreshing the page always fixes it. I have no idea what's making this happen. The registration pattern is standard:

Future<void> main() async {
  setPathUrlStrategy();

  sl
    ..registerSingletonAsync(() => SharedPreferencesService().init())
    ..registerSingletonAsync(
      () => UserService().init(),
      dependsOn: <Type>[SharedPreferencesService],
    );

  await sl.allReady();

  runApp(const App());
}
@escamoteur
Copy link
Collaborator

Does this happen in debug too? How easy is it to reproduce?

Does it also happen if you, instead of using a global variable "SL" use GetIt.I<> ?

@stx
Copy link
Author

stx commented Feb 16, 2022

@escamoteur Happens in debug mode only. GetIt.I<> does not appear to fix it.

We can make it happen somewhat frequently just by testing futures in main() and hot restarting. It feels very much like a framework-level race condition on hot restarts. The services reference each other and are registered via registerSingletonAsync which feels related, based on the race condition nature and the fact we've never experienced it on non-async singletons.

Future<void> main() async {
  sl
    ..registerSingletonAsync(() => DatabaseService().init())
    ..registerSingletonAsync(
      () => UserService().init(),
      dependsOn: <Type>[DatabaseService],
    );

  await sl.allReady();

  Timer.periodic(
    const Duration(seconds: 5),
    (_) => sl<DatabaseService>().doSomething(),
  );

  runApp(MaterialApp());
}

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 16, 2022 via email

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

No branches or pull requests

2 participants