Flutter:如何收听 FirebaseUser 是电子邮件验证布尔值?

eif*_*mon 15 state-management dart firebase firebase-authentication flutter

我的想法: 我想在 Flutter 中使用 Firebase Auth 插件来注册用户。但在他们可以访问应用程序之前,他们必须验证他们的电子邮件地址。因此,我在注册后将 Firebase 用户推送到验证屏幕。这只是一个加载屏幕,告诉用户他必须验证他的电子邮件。

但是现在:如果用户的电子邮件是否经过验证并将他(如果为真)发送到主屏幕,我该如何持续收听?

我是 Flutter 的新手,我不知道我是否必须使用 Streams 或 Observables 或 while Loop 或 setState() 或其他东西来进行这样的布尔检查。而且我也不知道如何设置解决方案。

这是我注册用户的基本代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final Firestore _db = Firestore.instance;

  Future<FirebaseUser> get getUser => _auth.currentUser();

  Stream<FirebaseUser> get user => _auth.onAuthStateChanged;

  Future<FirebaseUser> edubslogin(String email, String password) async {
    try {
      final FirebaseUser user = await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
     
      await user.sendEmailVerification();
      
      //email verification somewhere here
    
      updateUserData(user);
      return user;
    } catch (error) {
      print(error);
      return null;
    }
  }
Run Code Online (Sandbox Code Playgroud)

我试过这个:

     if (user.isEmailVerified == true) {
        
        //go to Homescreen
        return true; 
      } else {

        //show verification screen(loading spinner)
        return false;
      }
Run Code Online (Sandbox Code Playgroud)

但我没有trueisEmailVerified.

我需要做什么?

小智 18

我只是在我的应用程序中遇到了同样的情况。我的解决方案是在战略路线的 initState 方法中创建一个定期计时器,以保留应用程序,直到验证电子邮件。它不像使用侦听器那么优雅,但工作正常。

bool _isUserEmailVerified;
Timer _timer;

@override
void initState() {
    super.initState();
    // ... any code here ...
    Future(() async {
        _timer = Timer.periodic(Duration(seconds: 5), (timer) async {
            await FirebaseAuth.instance.currentUser()..reload();
            var user = await FirebaseAuth.instance.currentUser();
            if (user.isEmailVerified) {
                setState((){
                    _isUserEmailVerified = user.isEmailVerified;
                });
                timer.cancel();
            }
        });
    });
}

@override
void dispose() {
    super.dispose();
    if (_timer != null) {
        _timer.cancel();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • `导入'dart:异步';` (3认同)

Eri*_*ett 11

这种验证并不像您希望的那么简单。首先,存在识别用户已验证其电子邮件的问题。其次,存在的问题是,您无法收听任何类型的通知来自动触发您的应用程序中的更改。

检查此线程以获取有关 emailVerified 的信息:https : //github.com/flutter/flutter/issues/20390#issuecomment-514411392

我只能在以下情况下验证用户:1)创建他们的帐户,2)登录他们,3)然后检查以确保他们验证了他们的电子邮件。

final FirebaseAuth _auth = FirebaseAuth.instance;

var _authenticatedUser = await _auth.signInWithEmailAndPassword(email: _email, password: _password); 

//where _email and _password were simply what the user typed in the textfields.



if (_authenticatedUser.isEmailVerified) {
        //Verified
      } else {
        //Not verified
        }
Run Code Online (Sandbox Code Playgroud)

第 2 部分:如何让您的应用识别出用户已确认他们的电子邮件?找到一种方法来触发检查确认的函数。一个按钮很容易。如果您希望它看到“自动”,那么我想您可以创建一个计时器,每 10 秒左右检查一次电子邮件验证。

  • 这里的关键是,当用户验证其电子邮件地址时,应用程序不会自动收到通知,因此您需要在应用程序内自行检查。为此,您需要[强制重新加载](https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseUser/reload.html)用户配置文件,以便您从服务器。有了这些,您就可以重新检查“isEmailVerified”的值。 (4认同)