简体   繁体   English

如何使用 Flutter 和 Firebase 验证用户 email

[英]How to verify user email usign Flutter and Firebase

I was following a guide on medium.com about this particular topic and created a class to manage auth stuff but it gives me an error.我正在关注关于这个特定主题的 medium.com 指南,并创建了一个 class 来管理身份验证内容,但它给了我一个错误。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:lindo_app/models.dart/user.dart';


class AuthService {

  final FirebaseAuth _auth = FirebaseAuth.instance;

//auth change user stream

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

//create user object

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

  //Sign In with Email and Pass
  Future signInWithEmailAndPassword(String email, String password) async {
    try {
      AuthResult result = await _auth.signInWithEmailAndPassword(email: email, password: password);
      FirebaseUser user = result.user;
      return _userFromFirebaseUser(user);
    }
    catch(e){
      return null;
    }
  }

  //sign out
  Future signOut() async {
    try{
      return await _auth.signOut();
    }
    catch(error){
      print(error.toString(),);
      return null;
    }
  }

  // sign up

  Future signUp(String email, String password) async {
      FirebaseUser user = await _auth.createUserWithEmailAndPassword(email: email, password: password);
     try {
        await user.sendEmailVerification();
        return user.uid;
     } catch (e) {
        print("An error occured while trying to send email verification");
        print(e.message);
     }
  }
}

The error is:错误是:

AuthResult can't be assigned to FirebaseUser on line 47

(last method on the value of 'user' to be clear). (要明确“用户”值的最后一种方法)。 Thanks in advance提前致谢

The error is self-explanatory,you are assigning the returned value of createUserWithEmailAndPassword to a FirebaseUser variable while it returns an AuthResult该错误是不言自明的,您将createUserWithEmailAndPassword的返回值分配给FirebaseUser变量,同时它返回AuthResult

fix to your problem:解决您的问题:

Future signUp(String email, String password) async {
      AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
     FirebaseUser user = result.user;
     //do something with user
     try {
        await user.sendEmailVerification();
        return user.uid;
     } catch (e) {
        print("An error occured while trying to send email verification");
        print(e.message);
     }
  }

AuthResult can't be assigned to FirebaseUser on line 47无法在第 47 行将 AuthResult 分配给 FirebaseUser

You should call .user at the end of this line您应该在此行末尾调用.user

 FirebaseUser user = await _auth.createUserWithEmailAndPassword(email: email, password: password).user;

instead of代替

FirebaseUser user = await _auth.createUserWithEmailAndPassword(email: email, password: password);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM