简体   繁体   English

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

[英]This widget has been unmounted, so the State no longer has a context (and should be considered defunct). when Sign out from flutter app

I am using firebase with flutter to create the sign in methods.我正在使用 firebase 和 flutter 来创建登录方法。 However, When I signout from my flutter app i get this error但是,当我从 flutter 应用程序注销时,我收到此错误

This widget has been unmounted, so the State no longer has a context (and should be considered defunct).此小部件已卸载,因此 State 不再具有上下文(应视为已失效)。

and then when I sign in after this error, I get this error:然后当我在此错误后登录时,我收到此错误:

setState() or markNeedsBuild() called during build.在构建期间调用 setState() 或 markNeedsBuild()。

this is my code for sign out:这是我的退出代码:

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,
      );
    });

  }

And here is the sign in code:这是登录代码:

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());
     }

 }

I aprreciate any help我感谢任何帮助

You can check if it is mounted or not, then proceed您可以检查它是否已安装,然后继续

if (mounted) {
     /// all actions are here 
   }

This usually happens when you are changing state inside a page lower in tree and then trying to access it from higher level of page in case of provider package same goes for firebase you're probably signing in and status is not transferred to the parent page.当您在树中较低的页面内更改 state 然后尝试从更高级别的页面访问它时,通常会发生这种情况,以防提供者 package 同样适用于 ZBF12E1515C25C7D8C0352F1413AB9A15 并且状态可能未转移到您的父页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 flutter 错误:此小部件已卸载,因此 State 不再具有上下文(应被视为已失效) - flutter error: This widget has been unmounted, so the State no longer has a context (and should be considered defunct) Flutter 未处理的异常:此小部件已被卸载,因此 State 不再具有上下文(应被视为已失效) - Flutter Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct) Flutter:此小部件已被卸载,因此 State 不再具有上下文(并且应被视为已失效)使用 streambuilder 时出错 - Flutter : This widget has been unmounted, so the State no longer has a context (and should be considered defunct) Error when using streambuilder 变量错误设置state; 此小部件已被卸载,因此 State 不再具有上下文(应被视为已失效) - Set state for variable error; This widget has been unmounted, so the State no longer has a context (and should be considered defunct) 此小部件已卸载,因此 State 不再具有上下文(应视为已失效) - This widget has been unmounted, so the State no longer has a context (and should be considered defunct) popAndPushNamed - 未处理的异常:此小部件已被卸载,因此状态不再具有上下文(应视为已失效) - popAndPushNamed - Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct) SDK 和包升级时出现错误“此小部件已卸载,因此 State 不再具有上下文(并且应被视为已失效)” - Error "This widget has been unmounted, so the State no longer has a context (and should be considered defunct)" upon SDK and packages upgrade FlutterError(此小部件已卸载,因此 State 不再具有上下文 - FlutterError (This widget has been unmounted, so the State no longer has a context Flutter 错误:未处理的异常:此小部件已卸载,因此 State 不再具有上下文 - Flutter error: Unhandled Exception: This widget has been unmounted, so the State no longer has a context Flutter 未处理的异常:此小部件已卸载,因此 State 不再具有上下文 - Flutter Unhandled Exception: This widget has been unmounted, so the State no longer has a context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM