+-
我在调试控制台中获得了正确的行,但未反映在用户界面中。 (它不会切换页面并保持用户连接状态),但登录正常,但可以登录,但是当您无法注销并重新启动应用程序后,它将返回登录页面。
在主页中,我设置了一个流来收听FirebaseUser:
final FirebaseAuth _auth = FirebaseAuth.instance;
StreamProvider<FirebaseUser>.value(
value: _auth.onAuthStateChanged,
//...
然后我在Wrapper()
中设置一个main.dart
,以决定是否如下显示LoginScreen()
或HomeScreen()
:
final _user = Provider.of<FirebaseUser>(context);
if (_user == null) {
return LoginScreen();
} else if (_user != null) {
if (_user.isEmailVerified == true) {
return HomeScreen();
} else
return LoginScreen();
}
使用auth_service.dart中的流设置
更新:
错误出现在下面...
Notifying id token listeners about a sign-out event.
Notifying auth state listeners about a sign-out event.
user is not logged in
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Looking up
a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a
reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in
the widget's didChangeDependencies() method.
0
投票
投票
您不必执行所有这些操作,firebase身份验证可以处理登录并保持用户登录。在生命周期initState
的第一种方法中,您可以执行以下操作:
@override
void initState() {
super.initState();
var auth = FirebaseAuth.instance;
auth.onAuthStateChanged.listen((user) {
if (user != null) {
print("user is logged in");
//navigate to home page using Navigator Widget
} else {
print("user is not logged in");
//navigate to sign in page using Navigator Widget
}
});
}