This is my error: "Bad state: GetIt: Object/factory with type AuthRepository is not registered inside GetIt."

136 views Asked by At

I'm trying to use dependency injection with "get_it" and "injectable" dependencies, but I have some problems.

main.dart:

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
   await configureDependencies(); 

    runApp(const MyApp());   

}

app_module.dart:

@module
abstract class AppModule {

@preResolve //es prioritario
Future\<FirebaseService\> get firebaseService =\> FirebaseService.init();  // Inicializar firebase

@injectable
FirebaseAuth get firebaseAuth =\> FirebaseAuth.instance;

@injectable
FirebaseFirestore get firebaseFirestore =\> FirebaseFirestore.instance;

@injectable
CollectionReference get usersRef =\> firebaseFirestore.collection("Users");  //BASE DE DATOS DEL USUARIO EN FIREBASE

@Environment('repositories')
@injectable
AuthRepository get authRepository =\> AuthRepositoryImpl(firebaseAuth, usersRef);

@Environment('use_cases')  
@injectable
AuthUseCases get authUseCases =\> AuthUseCases(
login: LoginUsecase(authRepository),
register: RegisterUseCase(authRepository)
);

}



auth_repository_impl.dart:

@Environment('AuthRepositoryy')   //La he puesto yo
@Injectable(as:AuthRepository)
class AuthRepositoryImpl implements AuthRepository{

  FirebaseAuth _firebaseAuth;
  CollectionReference _usersRef;
  

  AuthRepositoryImpl(this._firebaseAuth, this._usersRef);

  @override
  Future<Resource> login({required String email, required String password}) async{
    try {
        UserCredential data = await _firebaseAuth.signInWithEmailAndPassword(email: email,password: password );
        return Success(data);
    } on FirebaseAuthException catch(e) {
      return Error(e.message ?? 'Error desconocido');

    }
  }

  @override
  Future<Resource> register(UserData user) async{
    try {
      UserCredential data =  await _firebaseAuth.createUserWithEmailAndPassword(
        email: user.email, 
        password: user.password
        );
         user.password = "";  //Eliminar la contraseƱa por seguridad (pasarla a un valor vacio)

         await _usersRef.doc(data.user?.uid ?? "").set(user.toJson()); //Registrar usuario en la base de datos de firebase

        return Success(data);             //'e' es la variable que capturamos del error
    } on FirebaseAuthException catch(e){  //error de tipo 'on FirebaseAuthException'
      return Error(e.message ?? "Error desocnocido");
    }
  }

}

injection.dart:

final locator = GetIt.instance;


@InjectableInit()
Future<void> configureDependencies() async => await locator.init();

I have tried a lot of things but I can't resolve the problem.

0

There are 0 answers