Firebase 身份验证

Firebase authenthication

我正在尝试将 firebase 用于我的 flutter 应用程序,但我的代码似乎有错误,我知道 firebaseuser 需要更改为用户,但它似乎不起作用,请帮助我,我是新手。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:quizmaker/view/signin.dart';
import 'package:quizmaker/models/user.dart';

class AuthService {
  FirebaseAuth _auth = FirebaseAuth.instance;

  Future signInEmailAndPass(String email, String password) async {
    try {
      UserCredential userCredential = await _auth.signInWithEmailAndPassword(
          email: email, password: password);
      User user = authResult.user; //the error is in this part
    } catch (e) {
      print(e.toString());
    }
  }
}

在此代码段中:

  UserCredential userCredential = await _auth.signInWithEmailAndPassword(
      email: email, password: password);
  User user = authResult.user;

您正在尝试使用从未定义的 authResult 变量。我最好的猜测是你想从 userCredential 获取用户,所以:

  UserCredential userCredential = await _auth.signInWithEmailAndPassword(
      email: email, password: password);
  User user = userCredential.user;

这仍然行不通,因为 userCredential.user 可能为空,因此定义为 User? 而不是 User。由于您使用的是 await,您可以确定 用户,因此您可以:

  User user = userCredential.user!;