The return type 'User?' isn't a 'User', as required by the closure's context. Firebase Auth

Viewed 252

I'm trying to wrap my head around implementing FireBase auth while following some tutorials (https://www.youtube.com/watch?v=_Sa60ZxFgSI&t=319s like this one), but recent Dart null-safety migration has been painful for me.

Currently stuck at writing User stream for tracking user auth state changes

User class:

class User {

  final String uid;
  
  User({ this.uid });

}

Auth Service:

import 'package:finances/models/user.dart' as u;
import 'package:firebase_auth/firebase_auth.dart';

class AuthenticationService {

  final FirebaseAuth _auth = FirebaseAuth.instance;

// create user obj based on firebase user
  u.User? _userFromFirebaseUser(User user) {
    return user != null ? u.User(uid: user.uid) : null;
  }

  Stream<User> get user {
    return _auth.authStateChanges().
      map((User firebaseUser) => (firebaseUser != null) ? u.User(uid: firebaseUser.uid) : null


    );
  }
  // sign in anon
  Future signInAnon() async {
    try {
      UserCredential result = await _auth.signInAnonymously();
      User? user = result.user;
      return _userFromFirebaseUser(user!);
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

The samples are pretty basic.

Errors:

The argument type 'User Function(User)' can't be assigned to the parameter type 'User Function(User?)'. (Documentation)
The return type 'User?' isn't a 'User', as required by the closure's context. 

and some more errors in terminal:

Performing hot reload...
Syncing files to device Android SDK built for x86 64...
lib/services/auth_service.dart:15:57: Error: A value of type 'User/*1*/?' can't be returned from a function with return type 'User/*2*/'.
 - 'User/*1*/' is from 'package:finances/models/user.dart' ('lib/models/user.dart').
 - 'User/*2*/' is from 'package:firebase_auth/firebase_auth.dart' ('/D:/flutterSDK/flutter_windows_2.8.1-stable/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-3.3.8/lib/firebase_auth.dart').
      map((User firebaseUser) => (firebaseUser != null) ? u.User(uid: firebaseUser.uid) : null
                                                        ^
lib/services/auth_service.dart:15:11: Error: The argument type 'User Function(User)' can't be assigned to the parameter type 'User Function(User?)' because 'User?' is nullable and 'User' isn't.
 - 'User' is from 'package:firebase_auth/firebase_auth.dart' ('/D:/flutterSDK/flutter_windows_2.8.1-stable/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-3.3.8/lib/firebase_auth.dart').
      map((User firebaseUser) => (firebaseUser != null) ? u.User(uid: firebaseUser.uid) : null

Can someone give me a clue or hint on how to solve this issue? Same library naming after they deprecated FirebaseUser doesn't help as well.

1 Answers

In this code, your function takes a not nullable user as arg (but you are still checking whether it is null in the code of that function so it doesn't make sense to make it not nullable).

u.User? _userFromFirebaseUser(User user)
  return user != null ? u.User(uid: user.uid) : null;
}

It should be this:

u.User? _userFromFirebaseUser(User? user)

It is probably throwing an error because you are getting a nullable user from the result.user and then you are casting it as not nullable with the ! in the line underneath.

User? user = result.user;
return _userFromFirebaseUser(user!);
Related