I am following this blog post to send email verification to users after they signup and this is the problem I'm getting when executing sendEmailVerification()
The method 'sendEmailVerification' isn't defined for the type 'UserCredential'. Try correcting the name to the name of an existing method, or defining a method named 'sendEmailVerification
But the problem with the blog post is that somethings that were used are deprecated and I managed to solve most of them except this one.
Here is my code:- (You can see the import statements for the name of packages)
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');
}
}
}
Can you help me solve the issue? Thanks!
Try the following:
await user.user.sendEmailVerification();
Since createUserWithEmailAndPassword()
returns an instance of UserCredential
and since the class UserCredential
has a property called user
which returns an instance of User
.
Therefore you can use the property user
to call the method sendEmailVerification()
which is inside the class User
.
createUserEmailAndPassword() returns a Future<UserCredential>
, and a UserCredential object has no method called sendEmailVerification
. That's what the error message is complaining about.
If you have a UserCredential to work with, then you can request email verification like this using its user property like this:
try {
final credential = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
await credential.user.sendEmailVerification();
return credential.user;
} catch (e) {
print('Code was not sent!');
}
One thing to be aware of I couldn't get this to work until I tried using a different email address. Somehow my first email address got glitched out by firebase.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.