Error: The argument type 'User?' can't be assigned to the parameter type 'User' because 'User?' is nullable and 'User' isn't. *(NullSafety)*

Error: The argument type 'User?' can't be assigned to the parameter type 'User' because 'User?' is nullable and 'User' isn't. *(NullSafety)*

我正在学习 Flutter 的课程,我觉得课程有点旧,我正在尝试将旧代码升级到空安全,但由于我才刚刚开始学习null safety,我遇到了错误,我不知道为什么,如果你能帮忙我会很高兴。

"auth_event"

part of 'auth_bloc.dart';

abstract class AuthEvent extends Equatable {
  const AuthEvent();

@override
  bool? get stringify => true;
  @override
 List<User?> get props => [];
}

class AuthUserChanged extends AuthEvent {
  final auth.User? user;

  const AuthUserChanged({this.user});

  @override
  List<User?> get props => [user];
}

class AuthLogoutRequested extends AuthEvent {}

最终auth.User?用户;

上下文:'user' 指的是 属性,因此无法升级。

"auth_state"

part of 'auth_bloc.dart';

enum AuthStatus { unknown, authenticated, unauthenticated }

class AuthState extends Equatable {
  final auth.User? user;
  final AuthStatus status;

  const AuthState({
    this.user,
    this.status = AuthStatus.unknown,
  });

  factory AuthState.unknown() => const AuthState();

  factory AuthState.authenticated({required auth.User user}) {
    return AuthState(user: user, status: AuthStatus.authenticated);
  }

  factory AuthState.unauthenticated() =>
      const AuthState(status: AuthStatus.unauthenticated);

  @override
  bool? get stringify => true;

  @override
  List<Object?> get props => [user, status];
}

"auth_bloc"

import 'dart:async';
import 'package:basla/depolar/depolar.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;
import 'package:firebase_auth/firebase_auth.dart';
import 'package:meta/meta.dart';

part 'auth_event.dart';
part 'auth_state.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  final AuthRepository _authRepository;
  late StreamSubscription<auth.User?> _userSubscription;

  AuthBloc({
    @required AuthRepository authRepository,
  })  : _authRepository = authRepository,
        super(AuthState.unknown()) {
    _userSubscription =
        _authRepository.user.listen((user) => add(AuthUserChanged(user: user)));
  }

  @override
  Future<void> close() {
    _userSubscription.cancel();
    return super.close();
  }

  @override
  Stream<AuthState> mapEventToState(AuthEvent event) async* {
    if (event is AuthUserChanged) {
      yield* _mapAuthUserChangedToState(event);
    } else if (event is AuthLogoutRequested) {
      await _authRepository.logOut();
    }
  }

  Stream<AuthState> _mapAuthUserChangedToState(AuthUserChanged event) async* {
    yield event.user != null
        ? AuthState.authenticated(user: event.user)
        : AuthState.unauthenticated();
  }
}

(用户:event.user)

错误:无法将参数类型 'User?' 分配给参数类型 'User',因为 'User?' 可以为 null 而 'User' 不是。

请提醒我需要编辑的代码..

问题是您的 authenticated 状态被非空 auth.User 接受。所以,只需将其更新为:

factory AuthState.authenticated({required auth.User? user})

另一个解决方案是将 ! 添加到 event.user:

yield event.user != null
        ? AuthState.authenticated(user: event.user!)
        : AuthState.unauthenticated();

Anw,在您想要进一步了解 flutter/dart 更新之前,您应该阅读文档:https://dart.dev/null-safety/understanding-null-safety