Flutter:如何收听 FirebaseUser 是电子邮件验证布尔值? [英] Flutter: How to listen to the FirebaseUser is Email verified boolean?

查看:13
本文介绍了Flutter:如何收听 FirebaseUser 是电子邮件验证布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My Idea: I want to use the Firebase Auth Plugin in Flutter to register the users. But before they can access the App, they have to verify their Email address. Therefor I push the Firebase users after registration to a verification screen. This is just a loading screen which tells the user that he has to verify his email.

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

But now: How can I continuously listen, if the users email is verified or not and send him (when true) to the Homescreen?

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

I'm new to Flutter and I don't know if I have to use a Streams or Observables or a while Loop or setState() or something else for such a boolean check. And I also don't know how to setup a solution.

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

This is my basic code for register a user:

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;
    }
  }

我已经试过了:

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

        //show verification screen(loading spinner)
        return false;
      }

但我没有从 isEmailVerified 中得到布尔值 true.

But I don't get a boolean value true out of isEmailVerified.

我该怎么办?

推荐答案

这种验证并不像您希望的那么简单.首先,存在识别用户已验证其电子邮件的问题.其次,存在的问题是,没有任何类型的通知可以自动触发您的应用更改.

This verification isn't as straightforward as you'd hope. First, there is the problem of recognizing that the user has verified their email. Second, there is the issue that there isn't any sort of a notification you can listen to that will automatically trigger a change in your app.

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

Check this thread for info about emailVerified: https://github.com/flutter/flutter/issues/20390#issuecomment-514411392

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

I was only able to verify the user if I 1) Created their account, 2) Signed them in, 3) Then checked to make sure they verified their email.

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
        }

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

Part 2: How do you get your app to recognize that the user has confirmed their email? Find a way to trigger the function that checks confirmation. A button would be easy enough. If you want it to see "automatic" then I guess you could create a timer that checks for email verification every 10 seconds or so.

这篇关于Flutter:如何收听 FirebaseUser 是电子邮件验证布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆