Bloc依赖于未注册的类型- Injectable



我使用可注入的库根据教程在youtube的一些注释替换。但我访问了pub.dev中的可注入库,查看了更改日志并替换了>RegisterAs(Type)按>可注入(作为:类型),但它不工作,并给出未注册的错误

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();
}

这里我实现了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();
}
}

这里是集团

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

创建完成后显示

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?
------------------------------------------------------------------------

假设我们遵循了相同的教程,您应该有一个IAuthFacade的具体实现,称为FirebaseAuthFacade

我正在使用比教程更新的包版本,但我不能透露它们。

为我工作需要两个步骤:

首先,替换FirebaseAuthFacade上的注释:
@prod
@lazySingleton
@RegisterAs(IAuthFacade)
class FirebaseAuthFacade implements IAuthFacade {

:

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

其次,向SignInFormBloc构造函数添加一个命名注释:

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

免责声明:这可能无法实现相同的行为。

Just Replace@lazySingleton@Injectable(如:IAuthFacade)with@LazySingleton(as: IAuthFacade)通过遵循官方文档https://pub.dev/packages/injectable。您可以在下找到此解决方案,将抽象类绑定到实现部分。

完整的解决方案:

@LazySingleton(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();
}
}

最新更新