Firebase通过谷歌id自动登录断言调试错误



我还是个新手,正在了解扑动,所以请有人告诉我哪个错了。我的自动登录代码如下:

class _login_pageState extends State<login_page> {
void initState() {
super.initState();
final auth = FirebaseAuth.instance;
print(auth.currentUser);
if (auth.currentUser !=null)
{
Navigator.of(context).push(MaterialPageRoute(builder: (context) =>main_bone()));
}
// Enable virtual display.
}
@override
Widget build(BuildContext context) {
...

我的意图是在auth.currentUser存在时自动登录,并导航到main_bone()类。通过打印代码,我确认身份验证正确获得登录用户信息。

我收到的错误信息是这样的。

Exception caught by widgets library =======================================================
The following assertion was thrown building Builder:
setState() or markNeedsBuild() called during build.
...

'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5127 pos 12: '!_debugLocked': is not true.

我多次看到这些错误信息,但仍然不完全理解为什么会出现这种情况。请告诉我我做错了什么以及为什么会出现这些错误。

尝试使用Firebase提供的StreamBuilderuserChanges来侦听身份验证事件。试试你的主要功能

home: StreamBuilder(
stream: FirebaseAuth.instance.userChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.hasData) {
return main_bone();
} else if (snapshot.hasError) {
return Center(
child: Text('${snapshot.error}'),
);
}
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return const LoginPage();
},
), 

相关内容

最新更新