I am writing an app using clean architecture and using GetIt to handle the dependency Injection, now every time i try to start the app it gives this error , telling me that i didn't register 'UserRemoteDataSourceImpl ' when i already did , you can see in the code below
════════ Exception caught by widgets library ═══════════════════════════════════
The following StateError was thrown building Builder:
Bad state: GetIt: Object/factory with type UserRemoteDataSourceImpl 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?)
Full Error:
E/GooglePlayServicesUtil( 7978): GooglePlayServices not available due to error 9
W/Firestore( 7978): (24.5.0) [GrpcCallProvider]: Failed to update ssl context: com.google.android.gms.common.GooglePlayServicesNotAvailableException
I/flutter ( 7978): Error while creating BaseAuthFirebaseRepository
I/flutter ( 7978): Stack trace:
I/flutter ( 7978): #0 throwIfNot
I/flutter ( 7978): #1 _GetItImplementation._findFactoryByNameAndType
I/flutter ( 7978): #2 _GetItImplementation.get
I/flutter ( 7978): #3 _GetItImplementation.call
I/flutter ( 7978): #4 InjectorContainer.init.<anonymous closure>
I/flutter ( 7978): #5 _ServiceFactory.getObject
I/flutter ( 7978): #6 _GetItImplementation.get
I/flutter ( 7978): #7 _GetItImplementation.call
I/flutter ( 7978): #8 InjectorContainer.init.<anonymous closure>
I/flutter ( 7978): #9 _ServiceFactory.getObject
I/flutter ( 7978): #10 _GetItImplementation.get
I/flutter ( 7978): #11 _GetItImplementation.call
I/flutter ( 7978): #12 InjectorContainer.init.<
I/flutter ( 7978): Error while creating GetCurrentUserUseCase
I/flutter ( 7978): Stack trace:
I/flutter ( 7978): #0 throwIfNot
I/flutter ( 7978): #1 _GetItImplementation._findFactoryByNameAndType
I/flutter ( 7978): #2 _GetItImplementation.get
I/flutter ( 7978): #3 _GetItImplementation.call
I/flutter ( 7978): #4 InjectorContainer.init.<anonymous closure>
I/flutter ( 7978): #5 _ServiceFactory.getObject
I/flutter ( 7978): #6 _GetItImplementation.get
I/flutter ( 7978): #7 _GetItImplementation.call
I/flutter ( 7978): #8 InjectorContainer.init.<anonymous closure>
I/flutter ( 7978): #9 _ServiceFactory.getObject
I/flutter ( 7978): #10 _GetItImplementation.get
I/flutter ( 7978): #11 _GetItImplementation.call
I/flutter ( 7978): #12 InjectorContainer.init.<
I/flutter ( 7978): Error while creating AuthCubit
I/flutter ( 7978): Stack trace:
I/flutter ( 7978): #0 throwIfNot
I/flutter ( 7978): #1 _GetItImplementation._findFactoryByNameAndType
I/flutter ( 7978): #2 _GetItImplementation.get
I/flutter ( 7978): #3 _GetItImplementation.call
I/flutter ( 7978): #4 InjectorContainer.init.<anonymous closure>
I/flutter ( 7978): #5 _ServiceFactory.getObject
I/flutter ( 7978): #6 _GetItImplementation.get
I/flutter ( 7978): #7 _GetItImplementation.call
I/flutter ( 7978): #8 InjectorContainer.init.<anonymous closure>
I/flutter ( 7978): #9 _ServiceFactory.getObject
I/flutter ( 7978): #10 _GetItImplementation.get
I/flutter ( 7978): #11 _GetItImplementation.call
I/flutter ( 7978): #12 InjectorContainer.init.<
════════ Exception caught by widgets library ═══════════════════════════════════
The following StateError was thrown building Builder:
Bad state: GetIt: Object/factory with type UserRemoteDataSourceImpl 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?)
The relevant error-causing widget was
MaterialApp
When the exception was thrown, this was the stack
#0 throwIfNot
#1 _GetItImplementation._findFactoryByNameAndType
#2 _GetItImplementation.get
#3 _GetItImplementation.call
#4 InjectorContainer.init.<anonymous closure>
#5 _ServiceFactory.getObject
#6 _GetItImplementation.get
#7 _GetItImplementation.call
#8 InjectorContainer.init.<anonymous closure>
#9 _ServiceFactory.getObject
#10 _GetItImplementation.get
#11 _GetItImplementation.call
#12 InjectorContainer.init.<anonymous closure>
#13 _ServiceFactory.getObject
#14 _GetItImplementation.get
#15 _GetItImplementation.call
#16 EuroImpiantiSrls.build.<anonymous closure>
#17 _CreateInheritedProviderState.value
#18 _InheritedProviderScopeElement.value
#19 Provider.of
#20 ReadContext.read
#21 _BlocBuilderBaseState.initState
#22 StatefulElement._firstBuild
#23 ComponentElement.mount
... Normal element mounting (220 frames)
#243 Element.inflateWidget
#244 MultiChildRenderObjectElement.inflateWidget
#245 MultiChildRenderObjectElement.mount
... Normal element mounting (426 frames)
#671 _InheritedProviderScopeElement.mount
... Normal element mounting (7 frames)
#678 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#685 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#692 _NestedHookElement.mount
... Normal element mounting (7 frames)
#699 _InheritedProviderScopeElement.mount
... Normal element mounting (7 frames)
#706 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#713 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#720 _NestedHookElement.mount
... Normal element mounting (7 frames)
#727 _InheritedProviderScopeElement.mount
... Normal element mounting (7 frames)
#734 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#741 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#748 _NestedHookElement.mount
... Normal element mounting (7 frames)
#755 _InheritedProviderScopeElement.mount
... Normal element mounting (7 frames)
#762 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#769 SingleChildWidgetElementMixin.mount
... Normal element mounting (7 frames)
#776 _NestedHookElement.mount
... Normal element mounting (7 frames)
#783 SingleChildWidgetElementMixin.mount
... Normal element mounting (33 frames)
#816 Element.inflateWidget
#817 Element.updateChild
#818 RenderObjectToWidgetElement._rebuild
#819 RenderObjectToWidgetElement.mount
#820 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure>
#821 BuildOwner.buildScope
#822 RenderObjectToWidgetAdapter.attachToRenderTree
#823 WidgetsBinding.attachRootWidget
#824 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure>
#828 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189:12)
(elided 3 frames from class _Timer and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════
Even when i already registered it:
----------
##
- `Heading`
##
GetIt sl = GetIt.instance;
class InjectorContainer {
static void init() {
//! Firebase
sl.registerLazySingleton<FirebaseFirestore>(
() => FirebaseFirestore.instance);
sl.registerLazySingleton<FirebaseAuth>(() => FirebaseAuth.instance);
sl.registerLazySingleton<FirebaseMessaging>(
() => FirebaseMessaging.instance);
//! datasources
sl.registerLazySingleton<BaseUserRemoteDataSource>(() =>
UserRemoteDataSourceImpl(sl<FirebaseFirestore>(), sl<FirebaseAuth>(),
sl<FirebaseMessaging>()));
sl.registerLazySingleton<BaseProductsRemoteDataSources>(
() => ProductsRemoteDataSources(sl(), sl()));
//! repository
sl.registerLazySingleton<BaseAuthFirebaseRepository>(
() => UserAuthRepositoryImpl(sl()));
sl.registerLazySingleton<BaseProductsPublicRepository>(
() => ProductPublicRepositoryImpl(sl()));
//! usecases
sl.registerLazySingleton(() => GetCurrentUserUseCase(sl()));
sl.registerLazySingleton(() => IsSignedInUseCase(sl()));
sl.registerLazySingleton(() => LoginWithPhoneUseCase(sl()));
sl.registerLazySingleton(() => OTPVerificationUseCase(sl()));
sl.registerLazySingleton(() => SignOutUserUseCase(sl()));
sl.registerLazySingleton(() => SignUpWithPhoneUseCase(sl()));
sl.registerLazySingleton(() => GetCurrentUidUseCase(sl()));
sl.registerLazySingleton(() => UpdateLastLoginTimeUseCase(sl()));
sl.registerLazySingleton(() => GetHomeScreenProductsUseCase(sl()));
sl.registerLazySingleton(() => GetFavouriteProductsUseCase(sl()));
sl.registerLazySingleton(() => GetProductImagesUseCase(sl()));
//! Cubit / Bloc
sl.registerFactory<NavBarScreensBloc>(() => NavBarScreensBloc());
sl.registerFactory<AuthCubit>(
() => AuthCubit(sl(), sl(), sl(), sl(), sl()));
sl.registerFactory<UserCubit>(() => UserCubit(sl(), sl(), sl()));
sl.registerFactory<PublicProductsCubit>(
() => PublicProductsCubit(sl(), sl(), sl()));
}
}
and here is the Code: UserRemoteDataSourceImpl
abstract class BaseUserRemoteDataSource {
Future<bool> isSignedIn();
Future<void> signOut();
Future<String?> loginWithPhone(String phoneNum);
Future<void> signUpWithPhone(UserEntity userEntity);
Future<bool> otpVerification(String otpNum, String verificationId);
Future<UserEntity> getCurrentUser();
Future<String> getCurrentUid();
Future<void> updateLastLoginTime();
Future<String> getFcmToken();
}
class UserRemoteDataSourceImpl extends BaseUserRemoteDataSource {
final FirebaseAuth auth;
final FirebaseFirestore firestore;
final FirebaseMessaging firebaseMessaging;
UserRemoteDataSourceImpl(this.firestore, this.auth, this.firebaseMessaging);
@override
Future<UserEntity> getCurrentUser() async {
final String userUid = auth.currentUser!.uid;
return await firestore
.collection(kUserCollection)
.doc(userUid)
.get()
.then((snapShot) {
return UserModel.fromSnapshot(snapShot);
});
}
@override
Future<bool> isSignedIn() async => auth.currentUser?.uid != null;
@override
Future<String> loginWithPhone(String phoneNum) async {
final completer = Completer<String>();
await auth.verifyPhoneNumber(
phoneNumber: phoneNum,
verificationCompleted: (phoneAuthCredential) async {
await auth.signInWithCredential(phoneAuthCredential);
completer.complete(phoneAuthCredential.verificationId);
},
verificationFailed: (error) {},
codeSent: (verificationId, forceResendingToken) async {
//! complete waits untill the function returns a value
completer.complete(verificationId);
},
codeAutoRetrievalTimeout: (verificationId) {},
);
return completer.future;
}
@override
Future<bool> otpVerification(String otpNum, String verificationId) async {
final completer = Completer<bool>();
PhoneAuthCredential credentials = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: otpNum,
);
await auth.signInWithCredential(credentials).then((value) {
firestore
.collection(kUserCollection)
.doc(value.user!.uid)
.snapshots()
.listen((snapShot) {
if (snapShot.exists) {
completer.complete(false);
} else {
completer.complete(true);
}
});
});
return completer.future;
}
@override
Future<void> signUpWithPhone(UserEntity userEntity) async {
await firestore.collection(kUserCollection).doc(userEntity.uid).set({
kUserName: userEntity.name,
kUserPhone: userEntity.phone,
kUserUid: userEntity.uid,
kUserRegisterTime: Timestamp.now(),
});
}
@override
Future<void> signOut() async => await auth.signOut();
@override
Future<String> getCurrentUid() async => auth.currentUser!.uid;
@override
Future<void> updateLastLoginTime() async {
await firestore
.collection(kUserCollection)
.doc(auth.currentUser!.uid)
.update({
kLastLoginTime: Timestamp.now(),
kFcmToken: await getFcmToken(),
});
}
@override
Future<String> getFcmToken() async {
final completer = Completer<String>();
NotificationSettings settings = await firebaseMessaging.requestPermission(
alert: true,
badge: true,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
completer.complete(await firebaseMessaging.getToken());
}
return completer.future;
}
}
I think you should make sure that the object is registered before or not using