【问题标题】:Unhandled Exception: type 'FlutterError' is not a subtype of type 'Exception' in type cast未处理的异常:类型“FlutterError”不是类型转换中“异常”类型的子类型
【发布时间】:2021-01-17 01:05:34
【问题描述】:

我尝试在使用 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;
      });
    });
  }

处理按钮上的点击

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

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

【问题讨论】:

    标签: flutter


    【解决方案1】:

    这里不需要类型转换

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

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

    【讨论】:

    • 我是否将我的导航器放置在正确的位置,因为它在成功验证后没有被调用?
    • 返回 Future 并比较响应。如果响应不为空,则导航,否则显示 toast。
    • 我有一个 API 调用的 sn-p,您可以参考 pastebin.com/GpxvWsCk。
    猜你喜欢
    • 2021-10-19
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2019-08-03
    • 1970-01-01
    • 2022-11-13
    • 2021-10-11
    相关资源
    最近更新 更多