Skip to content

You tried to access an instance of tomSharedPreferences1 that is not ready yet #237

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
RLNat opened this issue Dec 14, 2021 · 1 comment
Closed

Comments

@RLNat
Copy link

RLNat commented Dec 14, 2021

Hi,

I am new to get_it package. I have this problem when I tried to access it in main.dart.

Unhandled Exception: 'package:get_it/get_it_impl.dart': Failed assertion: line 404 pos 9: 'instanceFactory.isReady': You tried to access an instance of tomSharedPreferences1 that is not ready yet

This is my code : (injection.dart)

final GetIt getIt = GetIt.instance;

Future setuppp() async {

getIt.registerSingletonAsync<TomSharedPreferences>(() async => TomSharedPreferences().init(), instanceName: "tomSharedPreferences1");

  final dataConnectionChecker = InternetConnectionChecker();

getIt.registerSingletonWithDependencies<TomNetworkInfo>(
        () => TomNetworkInfoImp(checker: dataConnectionChecker), instanceName: "tomNetworkInfo1",
    dependsOn: [InitDependency(TomSharedPreferences, instanceName: "tomSharedPreferences1")],
  );

getIt.registerSingletonWithDependencies<ConnectionStatusSingleton>(
        () {
          TomNetworkInfo sharedPreferences1 = getIt<TomNetworkInfo>(instanceName: "tomNetworkInfo1");
      return ConnectionStatusSingleton(networkInfo: sharedPreferences1);
    },
    dependsOn: [InitDependency(TomNetworkInfo, instanceName: "tomNetworkInfo1")],
  );

}

And I tried to access the variable SharedPreferences or NetworkInfo in main.dart and the errors appear :

Future<void> main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await setuppp();
  final userRepository = UserRepository();
  Bloc.observer = SimpleBlocObserver();

  var fds585 = getIt<TomSharedPreferences>(instanceName: "tomSharedPreferences1");
  var fsdfsq = getIt<TomSharedPreferences>();
  var fdsfsqd = getIt<TomNetworkInfo>();

  runApp(...)

}
@RLNat
Copy link
Author

RLNat commented Dec 14, 2021

I resolve the issue with the allReady() function :
await getIt.allReady();

and specify the instance name of the Object:
var fds585 = getIt<TomSharedPreferences>(instanceName: "tomSharedPreferences1");

@RLNat RLNat closed this as completed Dec 14, 2021
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

1 participant