此小部件已被卸载,因此State不再有上下文(应视为已失效).当从flutter应用程序注销时



我正在使用Firebase和Flutter来创建一个登录方法。然而,当我在Flutter应用程序中注销我的帐户时,我会收到以下错误:

This widget has been unmounted, so the State no longer has a context (and should be considered defunct).

然后当我在这个错误后登录时,我会得到这个错误:

setState() or markNeedsBuild() called during build.

这是我的注销代码:

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 the action is here
}

当您在树中较低级别的页面内更改状态,然后尝试从较高级别的页面访问该页面时,通常会发生这种情况。在提供程序包的情况下,您可能正在登录firebase,并且状态未转移到父页面。

相关内容

最新更新