无法检索 Firebase 用户

阿布舍克

我无法检索 Firebase 用户。下面是我的用户类,只是为了从 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'.

}

我用 **** 突出显示的错误部分

真实的RK

将返回类型更改为用户?和用户?(我假设这是您的自定义数据类型)

FirebaseAuth 当前用户返回 null 如果没有用户登录并且 User 有用户登录。由于 null 安全,User? 和用户是不同的类型。

例子:

Users? _userFromFirebase(User? user) {
    if (user == null) {
      return null;
    }
    return Users(uid: user.uid);
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章