Skip to content

Object of type SharedPreferences is not registered inside GetIt #198

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
Devlonoah opened this issue Jun 6, 2021 · 2 comments
Closed

Object of type SharedPreferences is not registered inside GetIt #198

Devlonoah opened this issue Jun 6, 2021 · 2 comments

Comments

@Devlonoah
Copy link

This is what goes into my injection_container.dart file


final sl = GetIt.instance;
Future init() async {

//Bloc
  sl.registerFactory(
    () => NumberTriviaBloc(
      inputConverter: sl(),
      concrete: sl(),
      random: sl(),
    ),
  );

//Data - source
  sl.registerLazySingleton<NumberTriviaLocalDataSource>(
      () => NumberTriviaLocalDataSourceImpl(sl()));


//External
  sl.registerLazySingleton(() async => await SharedPreferences.getInstance());
  sl.registerLazySingleton(() => client.Client());
  sl.registerLazySingleton<DataConnectionChecker>(
      () => DataConnectionChecker());
}

And this is how I use it in main.dart

void main() async{
WidgetsFlutterBinding.ensureInitialized();
await di.init();

runApp(MyApp());
}

This is the error i get in my console
Exception: Object of type SharedPreferences is not registered inside GetIt

@Devlonoah
Copy link
Author

I later got it solved by

Making the init function a asynchronous one using async

creating a variable to get an instance of Sharedpreference,

final spref =await SharedPreference.getInstance();

then sl.registerSingleton<SharedPreference>(() async => spref);

@escamoteur
Copy link
Collaborator

you should use registerAsyncSingleton for this

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