Skip to content

Register lazy singleton that depends on async lazy singleton #126

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
haashem opened this issue Nov 5, 2020 · 5 comments
Closed

Register lazy singleton that depends on async lazy singleton #126

haashem opened this issue Nov 5, 2020 · 5 comments

Comments

@haashem
Copy link

haashem commented Nov 5, 2020

OK, the title maybe is confusing but here it is the dependency:

UserPreferences --> IPreferences __> Preferences --> SharedPreferences

I have setup this in main function:

get.registerLazySingletonAsync<SharedPreferences>(
      () async => SharedPreferences.getInstance());
  get.registerLazySingleton<IPreferences>(
      () => Preferences(get<SharedPreferences>()));
  get.registerLazySingleton<UserPreferences>(
      () => UserPreferences(get<IPreferences>()));

But when when I try to access getIt<UserPreferences>() get_it complains I tried to access an instance ofSharedPreferences that was not easy yet.

how can I solve this async dependency?

@escamoteur
Copy link
Collaborator

use registerSingletonWithDependencies for the two latter ones

@haashem
Copy link
Author

haashem commented Nov 6, 2020

Thanks for hint
To achieve synchronous async registration:

get.registerSingletonAsync<SharedPreferences>(
      () => SharedPreferences.getInstance());
  get.registerSingletonWithDependencies<IPreferences>(
      () => Preferences(get<SharedPreferences>()),
      dependsOn: [SharedPreferences]);
  get.registerSingletonWithDependencies<UserPreferences>(
      () => UserPreferences(get<IPreferences>()),
      dependsOn: [IPreferences]);

then later:

void _checkToshowOnboardingPage(BuildContext context) async {
    await getIt.isReady<UserPreferences>();
    final userPreferences = getIt.get<UserPreferences>();
.
.
.

}

so this way I ensure UserPreferences is ready before trying to access it.
wouldn't be nice isReady returns the UserPreferences? so I don't have to call get

@escamoteur
Copy link
Collaborator

Great that this worked for you.

  1. returning the instance would be an idea but would be a breaking change. Also most of the time I just use allReady() at some place at startup, and I don't check after that with isReady.
  2. you can save some key strokes by omitting the get as GetIt is a callable class, like
 final userPreferences = getIt<UserPreferences>();

@haashem
Copy link
Author

haashem commented Nov 6, 2020

👍
It took me a hours to understand and debug the issue; I got fooled that I'm accessing synchronous instance. I appreciate if you can update the read me especially in dependsOn section. provide a hint: if you are trying to get a instance depends on async instance you MUST use allReadywith a FutureBuilder or await isReady<T>()to guarantee dependency is ready before accessing it.

Thanks for supporting this great library 😉

@escamoteur
Copy link
Collaborator

Ok will make this more clear!

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