无法从FirebaseUser Flutter检索用户

时间:2020-08-25 20:27:26

标签: android firebase flutter dart firebase-authentication

这是auth.dart

class AuthService implements AuthImplementaion {   
  final FirebaseAuth _auth = FirebaseAuth.instance;

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

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

  Future signInWithEmailAndPassword(String email, String password) async {
    try {
      UserCredential result = await _auth.signInWithEmailAndPassword(
          email: email, password: password);
      FirebaseUser user = result.user;
      return user;
    } catch (error) {
      print(error.toString());
      return null;
    }
  }

Future registerWithEmailAndPassword(String email, String password) async {
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(
          email: email, password: password);
      // ignore: deprecated_member_use
      FirebaseUser user = result.user;
      return _userFromFirebaseUser(user);
    } catch (error) {
      print(error.toString());
      return null;
    }
  }
  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (error) {
      print(error.toString());
      return null;
    }
  }
}

user.dart

class User {
  final String uid;

  User({this.uid});
}

这是我遇到的错误。

名称“用户”在库中定义 'package:example / model / user.dart'和 'package:firebase_auth / firebase_auth.dart'。 尝试对其中一个导入指令使用“作为前缀”,或对除其中一个导入以外的所有内容隐藏名称。

1 个答案:

答案 0 :(得分:0)

由于两个软件包都包含类User,因此您必须使用as前缀:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:your_project/user.dart' as UserModal;

然后您可以做:

 User user = FirebaseAuth.instance.currentUser;
 UserModal.User user = UserModal.User("name");

还要检查以下内容:

Undefined class 'FirebaseUser'