s3v3ns
s3v3ns

Reputation: 218

Flutter : New error after dart migrate and null safety

After running dart migrate I got a ton of errors, most of them I believe I have fixed, but the last one does not make sense to me.

 Stream<UserData> get user{
return _auth.authStateChanges()
    .asyncMap(_userFromFireBaseUser as FutureOr<UserData> Function(User?));

}

The problematic part is the .asyncMap... The part "as FutureOr<UserData> Function(User?));" Was added by the dart migrate.

The errors I am getting are

error: Undefined class 'FutureOr'. (undefined_class) and error: The argument type 'dynamic Function(User?)' can't be assigned to the parameter type 'FutureOr Function(User?)'.

Posting rest of the code as well, it is used for Authentication.

  class AuthService extends ChangeNotifier
  {

 // UserData _currentUser = AuthService();
  String? _uid;
  String? _email;

  String? get getUid => _uid;
  String? get getEmail => _email;


  final FirebaseAuth _auth = FirebaseAuth.instance;

  //Create user ojb based on firebaseuser

  UserData? _userFromFireBaseUser(User? user) {
    return user != null ? UserData(uid: user.uid) : null;
  }

  
  Stream<UserData> get user{
    return _auth.authStateChanges()
        .asyncMap(_userFromFireBaseUser as FutureOr<UserData> Function(User?));

  }

  //Get UID
  Future getCurrentUID() async
  {
    final currentUid = await _auth.currentUser!.uid;
    return currentUid;
  }

  //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;
    }
  }

What is causing the error?

Upvotes: 1

Views: 864

Answers (1)

Mol0ko
Mol0ko

Reputation: 3288

You can simply remove type cast to FutureOr<UserData> Function(User?) because _userFromFireBaseUser is already the function that gets User? and returns UserData?. To avoid returning null values from the stream just filter them with where. Use this code:

Stream<UserData> get user {
  return _auth.authStateChanges()
    .asyncMap(_userFromFireBaseUser)
    .where((user) => user != null)
    .map((user) => user!);
}

Upvotes: 1

Related Questions