fluttercommunity / get_it

Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur
https://pub.dev/packages/get_it
MIT License
1.32k stars 141 forks source link

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

Closed pen110 closed 10 months ago

pen110 commented 1 year ago

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 commented 1 year ago

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

AliAltiyev commented 1 year ago

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 commented 1 year ago

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

escamoteur commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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