Skip to content

7.6.0 Bad state: GetIt: Object/factory with type SettingsModel is not registered inside GetIt. (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance; Did you forget to register it?) #325

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
pen110 opened this issue May 15, 2023 · 6 comments

Comments

@pen110
Copy link

pen110 commented May 15, 2023

Bad state: GetIt: Object/factory with type Model is not registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?),

get_it: ^4.0.2 is ok . but 7.6.0 , i dont know why

@escamoteur
Copy link
Collaborator

can you provide a small repro project that shows that behaviour? I have to debug this to see what the reason is.

@AliAltiyev
Copy link

AliAltiyev commented Jul 13, 2023

I got also this exception with getIt ^7.6.0

Exception has occurred.
StateError (Bad state: GetIt: Object/factory with type SaveAppThemeUseCase is not registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?))

import 'package:data/data.dart';

Future<void> initDataLayer() async {
  //Product
  getIt.registerLazySingleton<RemoteDataSource>(
    () => RemoteDataSourceImpl(),
  );

  getIt.registerLazySingleton<ProductRepository>(
    () => ProductRepositoryImpl(getIt()),
  );

  getIt.registerLazySingleton<FetchProductsUseCase>(
    () => FetchProductsUseCase(repository: getIt()),
  );

  getIt.registerLazySingleton<FetchProductByIdUseCase>(
    () => FetchProductByIdUseCase(repository: getIt()),
  );

  getIt.registerLazySingleton<FetchMenuItemsUseCase>(
    () => FetchMenuItemsUseCase(productRepository: getIt()),
  );
  //Locale Storage
  getIt.registerSingleton<Box<ProductModel>>(
    await Hive.openBox(LocaleStorage.cart.name),
  );

  getIt.registerLazySingleton<LocaleDataSource>(
    () => LocaleDataSourseImpl(products: getIt()),
  );
  //Theme
  getIt.registerLazySingleton<ThemeRepository>(
    () => ThemeRepositoryImpl(localeDataSource: getIt<LocaleDataSource>()),
  );
//Here is throws exception
  getIt.registerLazySingleton<SaveAppThemeUseCase>(
    () => SaveAppThemeUseCase(themeRepository: getIt<ThemeRepository>()),
  );

  getIt.registerLazySingleton<GetAppThemeUseCase>(
    () => GetAppThemeUseCase(themeRepository: getIt<ThemeRepository>()),
  );

  //Cart

  getIt.registerLazySingleton<CartRepository>(
    () => CartRepositoryImpl(localeDataSource: getIt()),
  );

  getIt.registerLazySingleton<GetCartItemsUseCase>(
    () => GetCartItemsUseCase(cartRepository: getIt()),
  );

  getIt.registerLazySingleton<AddCartItemUseCase>(
    () => AddCartItemUseCase(cartRepository: getIt()),
  );

  getIt.registerLazySingleton<DeleteAllCartItemsUseCase>(
    () => DeleteAllCartItemsUseCase(cartRepository: getIt()),
  );

  getIt.registerLazySingleton<DeleteCartItemUseCase>(
    () => DeleteCartItemUseCase(cartRepository: getIt()),
  );
}

@oscarshaitan
Copy link

I just got the same issue, but is not all the time, that's why is difficult to debug

@escamoteur
Copy link
Collaborator

@AliAltiyev I just saw that you uses an async factory function in one of your registrations. I recommend reading the readme up from here https://github.com/fluttercommunity/get_it#asynchronous-singletons

@oscarshaitan do you uses any async calls while registering too?

Without a reproproject its hard for me to tell why this happens

@oscarshaitan
Copy link

Screenshot 2023-08-31 at 14 02 02
Yes I have this one, but the funny case is that where I always got the error dont use this dependency

@escamoteur
Copy link
Collaborator

@oscarshaitan actually that way its fine, first to await and then to register

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

4 participants