Skip to content

[Question \ bug ] registerSingleton dependencies #278

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
abdel-ships-it opened this issue May 27, 2022 · 1 comment
Closed

[Question \ bug ] registerSingleton dependencies #278

abdel-ships-it opened this issue May 27, 2022 · 1 comment

Comments

@abdel-ships-it
Copy link

How are we supposed to do this? I'm having a hard time having services registered with registerSingleton depend on each other.

Example

GetIt.I.registerSingleton<UserStateService>(UserStateService());
GetIt.I.registerSingleton<AppViewStateService>(AppViewStateService());

And UserStateService looks like this

class UserStateService {
  final appViewStateService = GetIt.I.get<AppViewStateService>();
}

This throws the following error

I/flutter ( 6537): Exception 'package:get_it/get_it_impl.dart': Failed assertion: line 372 pos 7: 'instanceFactory != null': Object/factory with  type AppViewStateService is not registered inside GetIt.
package:get_it/get_it_impl.dart:372
I/flutter ( 6537): (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;

I also tried this.

GetIt.I.registerSingletonWithDependencies<UserStateService>(() => UserStateService(), dependsOn: [AppViewStateService]);
GetIt.I.registerSingleton<AppViewStateService>(AppViewStateService());

Which throws this error

I/flutter ( 6537): Exception Invalid argument(s): Dependent Type AppViewStateService is not registered in GetIt

For now I'm forced to change the order of registrations, if there's another way please let me know.

@iandis
Copy link

iandis commented Jun 14, 2022

duplicate of #218

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

3 participants