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.36k stars 149 forks source link

Bad state: GetIt: Object/factory with type <?> is not registered inside GetIt #370

Closed yusufkecer closed 3 months ago

yusufkecer commented 3 months ago

I have been facing an error that I cannot solve since last night. When I run the callback function in the GenderAsset widget, the application gives an error. When I use GenderCubit instead of Locator.sl(), the error goes away. I leave the code and pages here. I'd be happy if you help.

Gender StflW

 Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => Locator.sl<GenderCubit>(),
      child: const _GenderView(),
    );
  }

Gender Widget

  body: Center(
        child: Padding(
          padding: const ProductPadding.ten(),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              GenderAsset(
                value: false,
                onChanged: (bool? value) {
                  print(context.read<GenderCubit>().genderValue); //error line
                },
                asset: AssetValue.female.value.lottie,
                gender: LocaleKeys.gender_fm.tr(),
                color: ProductColor().pink,
                icon: FontAwesomeIcons.venus,
              ),
            ],
          ),
        ),
      ),

init

@InjectableInit()
abstract final class Locator {
  static final sl = GetIt.instance;

  static Future<void> locateServices() async {
    sl.init();
  }
}

config

extension GetItInjectableX on _i174.GetIt { // initializes the registration of main-scope dependencies inside of GetIt   _i174.GetIt init({
    String? environment,
    _i526.EnvironmentFilter? environmentFilter,   }) {
    final gh = _i526.GetItHelper(
      this,
      environment,
      environmentFilter,
    );
    gh.lazySingleton<_i454.AppRouter>(() => _i454.AppRouter());
    gh.lazySingleton<_i46.BaseTheme>(() => _i46.BaseTheme());
    gh.factory<_i427.CacheMethods<_i906.BMIS>>(() => _i682.BMICache());
    gh.lazySingleton<_i162.ProductLocalization>(() => _i162.ProductLocalization(
          child: gh<_i409.Widget>(),
          key: gh<_i409.Key>(),
        ));
    gh.factory<_i427.CacheMethods<_i906.Users>>(() => _i575.UserCache());
    gh.factory<_i427.CacheMethods<_i906.Settings>>(() => _i138.AppCache());
    gh.factory<_i907.GenderCubit>(
        () => _i907.GenderCubit(genderValue: gh<_i110.GenderValue>()));
    return this;   } }

GenderValue

enum GenderValue {
  male,
  female,
}

GenderCubit

@injectable
class GenderCubit extends Cubit<GenderState> {
  GenderCubit({GenderValue? genderValue})
      : _genderValue = genderValue ?? GenderValue.male,
        super(SelectGender(genderValue: genderValue));

  GenderValue? _genderValue;

  void changeGender({GenderValue? newGender}) {
    _genderValue = newGender;
    emit(SelectGender(genderValue: _genderValue));
  }
}
escamoteur commented 3 months ago

You are using injectable which I don't really know. The error clearly tells you that the object you try to access is not registered inside get_it. It's for sure not a bug in get_it but somehow your object does not get correctly registered. Also why are you using get_it inside a provider? If you decide to use get_it together with provider you should it only use where you don't have a context. Or Alternatively just use get_it. I also recommend checking out watch_it as an alternative to using cubits Am 13. Aug. 2024, 20:45 +0100 schrieb Yusuf Keçer @.***>:

I have been facing an error that I cannot solve since last night. When I run the callback function in the GenderAsset widget, the application gives an error. When I use GenderCubit instead of Locator.sl(), the error goes away. I leave the code and pages here. I'd be happy if you help. Gender StflW Widget build(BuildContext context) { return BlocProvider( create: (context) => Locator.sl(), child: const _GenderView(), ); } Gender Widget body: Center( child: Padding( padding: const ProductPadding.ten(), child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ GenderAsset( value: false, onChanged: (bool? value) { print(context.read().genderValue); //error line }, asset: AssetValue.female.value.lottie, gender: LocaleKeys.gender_fm.tr(), color: ProductColor().pink, icon: FontAwesomeIcons.venus, ), ], ), ), ), init @InjectableInit() abstract final class Locator { static final sl = GetIt.instance;

static Future locateServices() async { sl.init(); } } config extension GetItInjectableX on _i174.GetIt { // initializes the registration of main-scope dependencies inside of GetIt _i174.GetIt init({ String? environment, _i526.EnvironmentFilter? environmentFilter, }) { final gh = _i526.GetItHelper( this, environment, environmentFilter, ); gh.lazySingleton<_i454.AppRouter>(() => _i454.AppRouter()); gh.lazySingleton<_i46.BaseTheme>(() => _i46.BaseTheme()); gh.factory<_i427.CacheMethods<_i906.BMIS>>(() => _i682.BMICache()); gh.lazySingleton<_i162.ProductLocalization>(() => _i162.ProductLocalization( child: gh<_i409.Widget>(), key: gh<_i409.Key>(), )); gh.factory<_i427.CacheMethods<_i906.Users>>(() => _i575.UserCache()); gh.factory<_i427.CacheMethods<_i906.Settings>>(() => _i138.AppCache()); gh.factory<_i907.GenderCubit>( () => _i907.GenderCubit(genderValue: gh<_i110.GenderValue>())); return this; } }

GenderValue enum GenderValue { male, female, } GenderCubit @injectable class GenderCubit extends Cubit { GenderCubit({GenderValue? genderValue}) : _genderValue = genderValue ?? GenderValue.male, super(SelectGender(genderValue: genderValue));

GenderValue? _genderValue;

GenderValue get genderValue => _genderValue!;

void changeGender2() { print("changeGender2"); }

void changeGender({GenderValue? newGender}) { _genderValue = newGender; emit(SelectGender(genderValue: _genderValue)); } } — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>