初始化器类型 'AuthRepository?' 无法分配给 flutter 中的字段类型 'AuthRepository'
The initializer type 'AuthRepository?' can't be assigned to the field type 'AuthRepository' in flutter
错误出现在 authRepository 部分的第 6 行(在第一个代码片段或主要代码部分)。
主要代码-
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository _authRepository;
var v;
AuthBloc({
@required AuthRepository? authRepository
}) : _authRepository = authRepository super(AuthState.unknown());
这里是 AuthRepository
-
的片段
class AuthRepository extends BaseAuthRepository {
final FirebaseFirestore _firebaseFirestore;
final auth.FirebaseAuth _firebaseAuth;
AuthRepository({
FirebaseFirestore? firebaseFirestore,
auth.FirebaseAuth? firebaseAuth,
}) : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance,
_firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;
您正在尝试将可为空的变量分配给不可为空的变量。为构造函数使用语法糖(this.var
语法)并使用位置参数。
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository _authRepository;
var v;
AuthBloc(this._authRepository) : super(AuthState.unknown());
}
其他解决方案
如果您仍想对私有成员使用所需的命名参数,请将 _authRepository
设为可为空的变量,这将需要在您使用该变量 (_authRepository
) 的任何地方进行更改。
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository? _authRepository;
var v;
AuthBloc({
@required AuthRepository? authRepository
}) : _authRepository = authRepository super(AuthState.unknown());
}
错误出现在 authRepository 部分的第 6 行(在第一个代码片段或主要代码部分)。
主要代码-
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository _authRepository;
var v;
AuthBloc({
@required AuthRepository? authRepository
}) : _authRepository = authRepository super(AuthState.unknown());
这里是 AuthRepository
-
class AuthRepository extends BaseAuthRepository {
final FirebaseFirestore _firebaseFirestore;
final auth.FirebaseAuth _firebaseAuth;
AuthRepository({
FirebaseFirestore? firebaseFirestore,
auth.FirebaseAuth? firebaseAuth,
}) : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance,
_firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;
您正在尝试将可为空的变量分配给不可为空的变量。为构造函数使用语法糖(this.var
语法)并使用位置参数。
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository _authRepository;
var v;
AuthBloc(this._authRepository) : super(AuthState.unknown());
}
其他解决方案
如果您仍想对私有成员使用所需的命名参数,请将 _authRepository
设为可为空的变量,这将需要在您使用该变量 (_authRepository
) 的任何地方进行更改。
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository? _authRepository;
var v;
AuthBloc({
@required AuthRepository? authRepository
}) : _authRepository = authRepository super(AuthState.unknown());
}