【问题标题】:The method 'sendEmailVerification' isn't defined for the type 'UserCredential'. Firebase flutter没有为“UserCredential”类型定义方法“sendEmailVerification”。 Firebase 颤动
【发布时间】:2021-01-25 04:15:30
【问题描述】:

我正在关注this blog post 在用户注册后向他们发送电子邮件验证,这是我在执行sendEmailVerification() 时遇到的问题

没有为“UserCredential”类型定义方法“sendEmailVerification”。 尝试将名称更正为现有方法的名称,或定义名为“sendEmailVerification”的方法

但是这篇博文的问题是,使用的东西已经被弃用了,我设法解决了大部分问题,除了这个。

这是我的代码:-(您可以查看包名称的导入语句)

    import 'dart:async';
    
    import 'package:firebase_auth/firebase_auth.dart' as auth;
    import 'package:cloud_firestore/cloud_firestore.dart';
    
    class Auth {
      final auth.FirebaseAuth _firebaseAuth = auth.FirebaseAuth.instance;
    
      Future<String> signUp(String email, String password) async {
        try {
          final user = await _firebaseAuth
              .createUserWithEmailAndPassword(email: email, password: password);
          try {
            await user.sendEmailVerification(); // <== The error happens here
            return user;
          } catch (e) {
            print('Code was not sent!');
          }
        } catch (e) {
          print('An error occurred');
        }
      }
    }

你能帮我解决这个问题吗?谢谢!

【问题讨论】:

    标签: firebase flutter dart firebase-authentication


    【解决方案1】:

    需要注意的一点是,在我尝试使用不同的电子邮件地址之前,我无法让它工作。不知何故,我的第一个电子邮件地址被 firebase 泄露了。

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      await user.user.sendEmailVerification();
      

      因为createUserWithEmailAndPassword() 返回一个UserCredential 的实例,并且因为类UserCredential 有一个名为user 的属性,它返回一个User 的实例。

      因此,您可以使用属性user 来调用类User 中的方法sendEmailVerification()

      【讨论】:

      【解决方案3】:

      createUserEmailAndPassword() 返回一个Future&lt;UserCredential&gt;,而UserCredential 对象没有称为sendEmailVerification 的方法。这就是错误消息所抱怨的内容。

      如果您有一个 UserCredential 可以使用,那么您可以使用它的 user 属性请求这样的电子邮件验证,如下所示:

      try {
          final credential = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
          await credential.user.sendEmailVerification();
          return credential.user;
      } catch (e) {
          print('Code was not sent!');
      }
      
      

      【讨论】:

        猜你喜欢
        • 2021-08-27
        • 2021-05-12
        • 1970-01-01
        • 2021-05-21
        • 2021-06-04
        • 2021-10-04
        • 1970-01-01
        • 2020-11-05
        • 2022-01-17
        相关资源
        最近更新 更多