断言失败:第 5070 行 pos 12:“<optimized out>”:不是 true

问题描述 投票:0回答:1

用户成功登录并保存状态后,我想隐藏登录屏幕并仅加载主屏幕,但最终出现错误

以下断言被抛出建筑 导航器-[GlobalObjectKey _WidgetsAppState#6686e](脏,依赖项:[UnmanagedRestorationScope,HeroControllerScope],状态: NavigatorState#c7e9f(股票代码:跟踪 1 个股票代码)): 'package:flutter/src/widgets/navigator.dart':断言失败:行 第5070章 12:'':不正确。

当令牌仍然有效时隐藏登录屏幕并仅加载主屏幕的正确方法是什么?

我的代码

主.dart

    class _MyAppState extends State<MyApp> {
 
       @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'What',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        scaffoldBackgroundColor: Palette.scaffold,
      ),
      // home: SignIn(),
      routes: {
        //Homepage and being controled by PagesProvider
        '/': (context) => SignIn(),
        'nav': (context) => NavScreen(),
        // add all routes with names here
      },
    );
  }
}

我的登录.dart

class SignIn extends StatefulWidget {
  const SignIn({Key key}) : super(key: key);

  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {
  ProgressDialog progressDialog;

  MsalMobile msal;
  bool isSignedIn = false;
  bool isLoading = true;

  @override
  void initState() {
    super.initState();
    MsalMobile.create('assets/auth_config.json', authority).then((client) {
      setState(() {
        msal = client;
      });
      refreshSignedInStatus();
    });
  }

  /// Updates the signed in state

  refreshSignedInStatus() async {
    bool loggedIn = await msal.getSignedIn();
    if (loggedIn) {
      isSignedIn = loggedIn;
      if (isSignedIn) {
        dynamic data = await handleGetAccount();
        dynamic token = await handleGetTokenSilently();
        dynamic result = token;
        SharedPreferences sharedPreferences =
            await SharedPreferences.getInstance();
        sharedPreferences.get("username");
        sharedPreferences.get("token");
        print('access token (truncated): ${result.accessToken}');
        Navigator.of(context).pop();
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(
            builder: (context) => NavScreen(),
          ),
        );
      }
      // Remaining code for navigation
    }
  }

  /// Gets a token silently.
  Future<dynamic> handleGetTokenSilently() async {
    String authority = "https://login.microsoftonline.com/$TENANT_ID";
    final result = await msal.acquireTokenSilent([SCOPE], authority);
    if (result != null) {
      // print('access token (truncated): ${result.accessToken}');
      SharedPreferences sharedPreferences =
          await SharedPreferences.getInstance();
      sharedPreferences.setString("token", result.accessToken);
      return result;
    } else {
      print('no access token');
      return null;
    }
  }

  /// Signs a user in
  handleSignIn() async {
    await msal.signIn(null, [SCOPE]).then((result) {
      // ignore: unnecessary_statements
      refreshSignedInStatus();
    }).catchError((exception) {
      if (exception is MsalMobileException) {
        logMsalMobileError(exception);
      } else {
        final ex = exception as Exception;
        print('exception occurred');
        print(ex.toString());
      }
    });
  }

  logMsalMobileError(MsalMobileException exception) {
    print('${exception.errorCode}: ${exception.message}');
    if (exception.innerException != null) {
      print(
          'inner exception = ${exception.innerException.errorCode}: ${exception.innerException.message}');
    }
  }

  /// Signs a user out.
  handleSignOut() async {
    try {
      print('signing out');
      await msal.signOut();
      print('signout done');
      refreshSignedInStatus();
    } on MsalMobileException catch (exception) {
      logMsalMobileError(exception);
    }
  }

  /// Gets the current and prior accounts.
  Future<dynamic> handleGetAccount() async {
    // <-- Replace dynamic with type of currentAccount
    final result = await msal.getAccount();
    if (result.currentAccount != null) {
      SharedPreferences sharedPreferences =
          await SharedPreferences.getInstance();
      sharedPreferences.setString("username", result.currentAccount.username);
      //print(result.currentAccount.username);
      return result.currentAccount;
    } else {
      print('no account found');
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    progressDialog  = ProgressDialog(context, type:ProgressDialogType.Normal, isDismissible: false, );
    return MaterialApp(
        home: new Scaffold(
      body: Builder(
        builder: (context) => Stack(
          fit: StackFit.expand,
          children: <Widget>[
            Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              child: Image.asset('assets/landing.webp',
                  fit: BoxFit.fill,
                  color: Color.fromRGBO(255, 255, 255, 0.6),
                  colorBlendMode: BlendMode.modulate),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                SizedBox(height: 10.0),
                Container(
                  width: 130.0,
                  child: Align(
                      alignment: Alignment.center,
                      child: RaisedButton(
                          shape: RoundedRectangleBorder(
                              borderRadius: new BorderRadius.circular(30.0)),
                          color: Color(0xffffffff),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Icon(
                                FontAwesomeIcons.microsoft,
                                color: Color(0xFF01A6F0),
                              ),
                              // Visibility(
                              //   visible: !isSignedIn,
                              SizedBox(width: 10.0),
                              Text(
                                'Sign in',
                                style: TextStyle(
                                    color: Colors.black, fontSize: 18.0),
                              ),
                              // child: RaisedButton(
                              //   child: Text("Sign In"),
                              //   onPressed: handleSignIn,
                              // ),
                              // ),
                            ],
                          ),
                          onPressed: () => {                         
                            progressDialog.show(),
                                handleSignIn(),
                                progressDialog.hide()
                              })),
                )
              ],
            ),
          ],
        ),
      ),
    ));
  }
}
flutter dart
1个回答
0
投票

只有登录成功后才应导航至主页

因为 Naviagation.Pop 等于后退按钮,用户可以手动完成

这是一个更好的方法:

在 main.dart 中添加以下内容:

  routes: {
      Homepage and being controled by PagesProvider 

        'nav': (context) => NavScreen(),
        'home': (context) => HomePage(),
         // add all routes with names here 
      },

在您的refreshSignedInStatus()中:

删除这个:

Navigator.of(context).pop();
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(
            builder: (context) => NavScreen(),
          ),
        );

添加这个:

  Navigator.pushNamed(context, 'nav');
© www.soinside.com 2019 - 2024. All rights reserved.