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

查看:175
本文介绍了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循环或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;
      }

但是我没有布尔值 true isEmailVerified 中有$ c>。

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.

检查此线程以获取信息关于电子邮件的验证: https://github.com/flutter/flutter/issues/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天全站免登陆