flutterdartinjectable

Bloc depends on unregistered type - Injectable


I'm using injectable lib according to a tutorial in youtube some of annotations replaced. but I visit injectable lib in pub.dev look to the changelog and replaced > RegisterAs(Type) by > Injectable(as:Type) but it not working and give unregistered error

abstract class IAuthFacade {
  Future<Either<AuthFailure, Unit>> registerWithEmailAndPassword(
      {required EmailAddress emailAddress, required Password password});
  Future<Either<AuthFailure, Unit>> signInWithEmailAndPassword(
      {required EmailAddress emailAddress, required Password password});
  Future<Either<AuthFailure, Unit>> signInWithGoogle();
}

And here I implemented the Interface

@lazySingleton
@Injectable(as: IAuthFacade)
class FirebaseAuthFacade implements IAuthFacade {
  final FirebaseAuth _auth;

  FirebaseAuthFacade(this._auth);

  @override
  Future<Either<AuthFailure, Unit>> registerWithEmailAndPassword({required EmailAddress emailAddress, required Password password}) {
    // TODO: implement registerWithEmailAndPassword
    throw UnimplementedError();
  }

  @override
  Future<Either<AuthFailure, Unit>> signInWithEmailAndPassword({required EmailAddress emailAddress, required Password password}) {
    // TODO: implement signInWithEmailAndPassword
    throw UnimplementedError();
  }

  @override
  Future<Either<AuthFailure, Unit>> signInWithGoogle() {
    // TODO: implement signInWithGoogle
    throw UnimplementedError();
  }
}

And here is the bloc

@injectable
class SignInFormBloc extends Bloc<SignInFormEvent, SignInFormState> {
  final IAuthFacade _authFacade;
}

After building it shows me

Missing dependencies in sabaclassesorganizer/injection.dart

[SignInFormBloc] depends on unregistered type [IAuthFacade] from package:sabaclassesorganizer/domain/auth/i_auth_facade.dart

Did you forget to annotate the above class(s) or their implementation with @injectable?
or add the right environment keys?
------------------------------------------------------------------------


Solution

  • assuming we followed the same tutorial, you should have a concrete implementation to the IAuthFacade called FirebaseAuthFacade.

    I am using newer package versions than the tutorial, but I can't disclose them. They are close to the latest as of writing this answer.

    what worked for me required two steps:

    first, replace the annotations on the FirebaseAuthFacade:

    @prod
    @lazySingleton
    @RegisterAs(IAuthFacade)
    class FirebaseAuthFacade implements IAuthFacade {
    

    with:

    @named
    @Injectable(as: AuthFacade)
    class FirebaseAuthFacade implements IAuthFacade {
    

    second, add a named annotation to the SignInFormBloc constructor:

    SignInFormBloc(@Named('FirebaseAuthFacade') this._authFacade) : super(SignInFormState.initial()) {
    

    disclaimer: this may not achieve the same behavior.