我无法检索Firebase用户。下面是我的Users类,只是为了从Firebase用户数据中获取所需的字段。
class Users {
final String uid;
Users({ required this.uid });
}
下面是我的Auth类:
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:codeproject/models/users.dart';
abstract class AuthBase {
Stream<Users> get authStateChanges;
Future<Users> currentUser();
Future<Users> signInAnonymously();
Future<Users> signInWithEmailAndPassword(String email, String password);
Future<Users> createUserWithEmailAndPassword(String email, String password);
Future<Users> signInWithGoogle();
Future<void> signOut();
}
class AuthService implements AuthBase {
final FirebaseAuth _auth = FirebaseAuth.instance;
Users _userFromFirebase(User user) {
if (user == null) {
**return null;**
}
return Users(uid: user.uid);
}
// ERROR: A value of type 'Null' can't be returned from the method '_userFromFirebase' because it has a return type of 'Users'.
@override
Stream<Users> get authStateChanges {
return _auth.authStateChanges().map(**_userFromFirebase**);
}
//ERROR: The argument type 'Users Function(User)' can't be assigned to the parameter type 'Users Function(User?)'.
@override
Future<Users> currentUser() async {
final user = _auth.currentUser;
return _userFromFirebase(**user**);
}
//ERROR: The argument type 'User?' can't be assigned to the parameter type 'User'.
@override
Future<Users> signInAnonymously() async {
final authResult = await _auth.signInAnonymously();
return _userFromFirebase(**authResult.user**);
}
//ERROR: The argument type 'User?' can't be assigned to the parameter type 'User'.
}
错误部分我用****突出显示
将返回类型更改为User?和用户?(我假设这是您的自定义数据类型)
FirebaseAuth current user返回null如果没有用户登录并且user is有用户登录。由于无效安全,用户?和User是不同的类型。
的例子:
Users? _userFromFirebase(User? user) {
if (user == null) {
return null;
}
return Users(uid: user.uid);
}