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


I’m taking a course for Flutter and I think the course is a bit old, I’m trying to upgrade an old code to null safety, but since I’m just starting to learn null safety, I’m encountering errors and I couldn’t figure out why, I would be glad if you could help.


part of 'auth_bloc.dart';

abstract class AuthEvent extends Equatable {
  const AuthEvent();

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

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

  const AuthUserChanged({this.user});

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

class AuthLogoutRequested extends AuthEvent {}

final auth.User? user;

Context: ‘user’ refers to a property so it couldn’t be promoted.


part of 'auth_bloc.dart';

enum AuthStatus { unknown, authenticated, unauthenticated }

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

  const AuthState({
    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);

  bool? get stringify => true;

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


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;

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

  Future<void> close() {
    return super.close();

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

(user: event.user)

Error: The argument type ‘User?’ can’t be assigned to the parameter type ‘User’ because ‘User?’ is nullable and ‘User’ isn’t.

  • ‘User’ is from ‘package:firebase_auth/firebase_auth.dart’

Please alert me for codes that i need to edit..


The problem is that your authenticated state is accepted by a not null auth.User. So, just update it to:

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

Another solution is adding ! to event.user:

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

Anw, you should read the documentation before you want to go any further with flutter/dart updates:

Answered By – Huy Nguyen

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *