Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

signalsReady doesn't work on registerSingletonWithDependencies #196

Closed
petro-i opened this issue May 26, 2021 · 5 comments
Closed

signalsReady doesn't work on registerSingletonWithDependencies #196

petro-i opened this issue May 26, 2021 · 5 comments

Comments

@petro-i
Copy link

petro-i commented May 26, 2021

Hi please see code sample below:

GetIt getIt = GetIt.instance;

void main() async {
  getIt.registerSingleton<Service1>(Service1(), signalsReady: true);
  getIt.registerSingletonWithDependencies<Service2>(() => Service2(),
      dependsOn: [Service1], signalsReady: true);

  await getIt.allReady();

  final service2 = getIt<Service2>();

  runApp(MyApp());
}
class Service1 {
  Service1() {
    _init();
  }

  _init() async {
    await Future.delayed(Duration(milliseconds: 1));
    getIt.signalReady(this);
  }
}

class Service2 {
  Service2() {
    _init();
  }

  _init() async {
    await Future.delayed(Duration(milliseconds: 1));
    getIt.signalReady(this);
  }
}`

as result on code `final service2 = getIt<Service2>();` 
exception is reported: `You tried to access an instance of Service2 that is not ready yet`

It is observed that `registerSingletonWithDependencies<Service2>` really awaits until Service1 reports ready, but allReady doesn't await for Service2 reports ready.

GetIt v.7.1.3
@stx
Copy link

stx commented Jun 9, 2021

Can confirm. @escamoteur Is there any workaround to this or an ETA on a fix?

@escamoteur
Copy link
Collaborator

I'm looking into it but it's a bit tricky to debug

@stx
Copy link

stx commented Jun 12, 2021

Found a workaround. registerSingletonAsync with dependsOn works as you’d expect.

@escamoteur
Copy link
Collaborator

indeed that's what I would have expected and how most people use it. But the other constellation should work too

@escamoteur
Copy link
Collaborator

ah, that was indeed a tricky one :-) I found and hopefully fixed in in V7.1.4

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

No branches or pull requests

3 participants