【问题标题】:FLutter FirebaseAuth Email verificationFLutter FirebaseAuth 电子邮件验证
【发布时间】:2021-05-13 11:00:16
【问题描述】:

现在我尝试编写自己的 android 应用程序。 因此我想创建一个基于电子邮件和密码的登录/注册,其中还包括一个电子邮件验证服务。 问题是,每次我在模拟器中启动它并尝试注册时,它都会发生一个 nosuchmethod 错误,告诉我 getter email 或 getter sendEmailVerification() 在 null 上调用。

请查看我的验证码并告诉我我做错了什么。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'home.dart';






class Verification extends StatefulWidget {
  @override
  _VerificationState createState() => _VerificationState();
}

class _VerificationState extends State<Verification> {
  final auth = FirebaseAuth.instance;
  User user;
  Timer timer;
  String email =  FirebaseAuth.instance.currentUser.email.toString(); 


 Future<void> verificateEmail() async{
   User user = auth.currentUser;

   if (user != null) {
     await user.sendEmailVerification();}
    timer = Timer.periodic(Duration(seconds: 2), (timer) {checkEmailVerified(); });

 }
    
   
  
  
  
  
  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }




  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("draicksmail sent to ${user.email}"),
      ),
    );
  }




  Future<void> checkEmailVerified() async{
    user = auth.currentUser;
    await user.reload();
    if (user.emailVerified){
      timer.cancel();
      Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context)=> Home()));
    }
  }


    
  

}

【问题讨论】:

  • 您能添加更多代码吗?不过从你所拥有的情况来看,auth.currentUser 是空的。
  • 谢谢兄弟,但我已经用最终的 auth = FirebaseAuth.instance 定义了 auth,之前有几行。顺便说一句,“Home()”扩展了一个在另一个 dart 文件中定义的无状态小部件,并且“Verification()”类被“Navigator.of(context).push(materialpageroutebuilder(builder: (context) => Verification ())),},
  • 那么当您执行以下操作时,您的user 是否已定义? User user = auth.currentUser; ?另外请添加您的错误消息
  • 是的用户已定义。
  • 错误信息是:NoSuchMethodError: The getter 'email' was called on null 尝试调用电子邮件 另请参阅:flutter.dev/docs/testing/errors

标签: android firebase flutter


【解决方案1】:

所以这个错误是因为auth.currentUser返回null造成的。因此,尝试在 null 上执行方法会引发错误,因为 null 上不存在任何内容。

您是否在main() 方法中添加了以下内容?

await Firebase.initializeApp();
runApp(Home());

查看以下指南了解更多详情https://firebase.google.com/docs/flutter/setup?platform=android

【讨论】:

  • 是的,我已将这些添加到我的主目录中
  • 初始化有没有报错信息?
  • 是的,抱歉,没有更多代码或信息,我被卡住了,很遗憾。根据您提供的代码应该可以工作。
猜你喜欢
  • 2018-08-26
  • 2021-06-24
  • 2020-07-16
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-01-16
相关资源
最近更新 更多