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

But now:如果用户的邮箱经过验证或未验证,我如何继续收听并将他(如果是真的)发送到主屏幕?

我还是个初学者,我不知道是否必须使用Streams或可观察对象、While循环或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;
    }
  }

我试过这个:

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

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

但我得不到isEmailVerified的布尔值true.

我该怎么做?

推荐答案

这种验证并不像你希望的那么简单.首先,识别用户已验证其邮箱的问题.第二个问题是,没有任何类型的通知可以自动触发应用程序的更改.

查看此线程以获取有关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
        }

第2部分:如何让您的应用程序识别用户已确认其邮箱?找到一种方法来触发判断确认的功能.一颗纽扣就足够简单了.如果你想让它显示为"自动",那么我想你可以创建一个计时器,每隔10秒左右判断一次邮箱验证.

Flutter相关问答推荐

Cupertino Buttino SheetAction on按下可触发加载指示器,然后勾选并延迟

自动测试Flutter应用程序进行QA的最佳方法是什么?

在Flutter 中将参数从一个类传递到另一个类

使用自定义控制器将项插入到ListView中时行为不正确

Android NFC未收到空的NFC标签

有没有一种正确的方法可以通过上下滑动在两个小部件(在我的情况下是应用程序栏)之间切换?

关闭模式,但再次打开时微件条件相同

Flutter API请求BadState响应

我有相关的下拉菜单Flutter Windows应用程序的问题

不要跨同步间隔使用BuildContext

在Dart中使用Riverpod代码生成时出现问题(addListener)

如何在 Flutter 中以背景 colored颜色 制作圆圈的一部分

如何在Flutter中显示ListView Builder区域外的列表数据?

不使用 GlobalKey 仍然收到 GlobalKey 被多次使用的错误

Flutter - firebase_auth_web 5.4.0 依赖于 intl ^0.17.0 但我必须使用更高版本 (intl ^0.18.0)

ListView 后台可见

Android Electric eel Mac M1:运行 flutter doctor -v 时无法找到Bundle 的 java 版本

Flutter RawMaterialButton 常量相对大小

如何使用 Row 和 Column 制作 L 形布局?

在 Flutter 中,您什么时候应该更喜欢Widget继承而不是组合?