Vinasirajan Vilakshan
Vinasirajan Vilakshan

Reputation: 327

A value of type 'Null' can't be returned from the method '_userFromFirebaseUser' because it has a return type of 'User'

import "package:firebase_auth/firebase_auth.dart";
import 'package:signup/models/user.dart';

class Auth {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  User _userFromFirebaseUser(FirebaseUser user) {
    if (user != null) {
      return User(uid: user.uid);
    } else {
      return null;
    }
  }

  Stream<User> get user {
    return _auth.onAuthStateChanged
        .map(_userFromFirebaseUser);
  }

  //anonomously

  Future signInAnon() async {
    try {
      AuthResult result = await _auth.signInAnonymously();
      FirebaseUser user = result.user;
      return _userFromFirebaseUser(user);
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
  //signin with pwd

  //register with pwd

  //sign-out

}

I went error while using _userFromFirebaseUser method. It is saying that it cant return a null value, if I crect it using auto corrector option

User? _userFromFirebaseUser(FirebaseUser user) {
    if (user != null) {
      return User(uid: user.uid);
    } else {
      return null;
    }
  }

But I got error in getter. Try to help me with that.

Upvotes: 2

Views: 759

Answers (1)

Thierry P. Oliveira
Thierry P. Oliveira

Reputation: 626

I don't know if you can do this, but is an option:

User _userFromFirebaseUser(FirebaseUser user) {
    User _user = new User();
    if (user != null)
        _user.uid = user.uid;
    return _user;
  }

With the response of that, you can check if de uid of user is null, so continue your flow.

Upvotes: 1

Related Questions