[英]flutter error: This widget has been unmounted, so the State no longer has a context (and should be considered defunct)
[英]This widget has been unmounted, so the State no longer has a context (and should be considered defunct). when Sign out from flutter app
我正在使用 firebase 和 flutter 来创建登录方法。 但是,当我从 flutter 应用程序注销时,我收到此错误
此小部件已卸载,因此 State 不再具有上下文(应视为已失效)。
然后当我在此错误后登录时,我收到此错误:
在构建期间调用 setState() 或 markNeedsBuild()。
这是我的退出代码:
Future<void> signOut() async {
if (googleSignIn.currentUser != null){
await googleSignIn.disconnect();
await FirebaseAuth.instance.signOut();}
else{
await FirebaseAuth.instance.signOut();
}
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => const AuthPage(),
),
(route) => false,
);
});
}
这是登录代码:
Future googleLogin() async{
timer1 = Timer(const Duration(milliseconds: 1500), (){
Navigator.of(context, rootNavigator: true).pop();
});
showDialog(context: context,
barrierDismissible: false,
builder: (context) => Center(
child: SizedBox(
width: 150, height: 150,
child: LiquidCircularProgressIndicator(
backgroundColor: const Color(0xFF5B84B1),
valueColor: const AlwaysStoppedAnimation(Colors.white),
),
),
),
).then((value) {
timer1!.cancel();
});
try {
final googleUser =
await GoogleSignIn(scopes: ['profile', 'email']).signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
UserCredential userCredential = await _auth.signInWithCredential(
credential);
if (userCredential.additionalUserInfo!.isNewUser == true) {
if (!mounted) return;
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => const ProfilePage(),
),
(route) => false,
);
}
else {
if (!mounted) return;
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => const HomeScreen(),
),
(route) => false,
);
}
} on PlatformException catch (e) {
Utils.showSnackBar(e.toString());
}
}
我感谢任何帮助
您可以检查它是否已安装,然后继续
if (mounted) {
/// all actions are here
}
当您在树中较低的页面内更改 state 然后尝试从更高级别的页面访问它时,通常会发生这种情况,以防提供者 package 同样适用于 ZBF12E1515C25C7D8C0352F1413AB9A15 并且状态可能未转移到您的父页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.