Reputation: 330
I'm learning Dependency Injection and Bloc. However, 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;
GenderValue get genderValue => _genderValue!;
void changeGender2() {
print("changeGender2");
}
void changeGender({GenderValue? newGender}) {
_genderValue = newGender;
emit(SelectGender(genderValue: _genderValue));
}
}
Upvotes: 0
Views: 75