My Idea:我想在 Flutter 中使用 Firebase Auth 插件来注册用户。
但在他们可以访问该应用程序之前,他们必须验证自己的电子邮件地址。
因此,我在注册后将 Firebase 用户推送到验证屏幕。这只是一个加载屏幕,告诉用户他必须验证他的电子邮件。
But now:如果用户的电子邮件已验证或未验证并将其发送(如果正确)到主屏幕,我如何才能持续收听?
我是 Flutter 新手,我不知道是否必须使用 Streams 或 Observables 或 while Loop 或 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;
}
但我没有得到布尔值true
out of isEmailVerified
.
我需要做什么?
我在我的应用程序中遇到了同样的情况。我的解决方案是在战略路线的 initState 方法中创建一个定期计时器,以保持应用程序直到电子邮件得到验证。它不像使用监听器那么优雅,但工作得很好。
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
class _AccountConfirmationState extends State<AccountConfirmation> {
late Timer _timer;
@override
void initState() {
super.initState();
_timer = Timer.periodic(const Duration(seconds: 5), (timer) async {
await FirebaseAuth.instance.currentUser?.reload();
final user = FirebaseAuth.instance.currentUser;
if (user?.emailVerified ?? false) {
timer.cancel();
Navigator.pop(context, true);
}
});
}
@override
void dispose() {
super.dispose();
_timer.cancel();
}
@override
Widget build(BuildContext context) {
//TODO: Implement your amazing waiting screen here
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)