首页 > 解决方案 > 未处理的异常:类型“FlutterError”不是类型转换中“异常”类型的子类型

问题描述

在使用 Flutter 成功登录后,我试图将用户重定向到主屏幕,但我最终得到了错误,并且返回到登录页面没有被重定向。

未处理的异常:类型“FlutterError”不是类型转换中“异常”类型的子类型

到目前为止我做了什么

我的登录功能

handleSignIn() async {
    await msal.signIn(null, [SCOPE]).then((result) {
      Navigator.push(context, MaterialPageRoute(builder: (context)=> Home()));
      refreshSignedInStatus();
    }).catchError((exception) {
      if (exception is MsalMobileException) {
        logMsalMobileError(exception);
      } else {
        final ex = exception as Exception;
        print('exception occurred');
        print(ex.toString());
      }
    });
  }

更新登录状态

  refreshSignedInStatus() {
    msal.getSignedIn().then((loggedIn) {
      print('refreshing');
      setState(() {
        isSignedIn = loggedIn;
      });
    });
  }

处理按钮上的onclick

 Text(' Sign in', style: TextStyle( color: Colors.black, fontSize: 18.0),
                              ),
                            ],
                          ),
                          onPressed: handleSignIn,
                        ),
                      )),

我做错了什么,提前感谢您的帮助

标签: flutter

解决方案


你在这里不需要 TypeCasting

final ex = exception as Exception; 此语法会导致错误

您可以简单地将异常分配给变量(不推荐),也可以直接使用该变量(推荐)。


推荐阅读