程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Flutter重定向到initState上的页面大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Flutter重定向到initState上的页面?

开发过程中遇到Flutter重定向到initState上的页面的问题如何解决?下面主要结合日常开发的经验,给出你关于Flutter重定向到initState上的页面的解决方法建议,希望对你解决Flutter重定向到initState上的页面有所启发或帮助;

尝试包装您的Navigator电话:

Navigator.of(context).pushnamed("login");

在预定的回调中addPostFrameCallBACk

schedulerBinding.instance.addPostFrameCallBACk((_) {
  Navigator.of(context).pushnamed("login");
});

您需要在文件顶部进行此导入:

import 'package:Flutter/scheduler.dart';

作为备选方案,虑一下,如果你可以只具有@H_742_3@myHomePage的build()方法返回一个LoginPage代替Scaffold,如果用户没有登录。这可能会与返回键相互作用更好,因为你不希望用户支持的登录对话框中做出来在完成登录之前。

解决方法

我有一个需要登录才能继续的应用程序(例如Google)。

当需要身份验证时,我想重定向用户。

但是,当我运行一个Navigator.of(context).pushNamed("myroute")。我收到以下错误:

 ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5624): The following assertion was thrown building _ModalScopeStatus(activE):
I/flutter ( 5624): setState() or markNeedsBuild() called during build.
I/flutter ( 5624): This Overlay widget cAnnot be marked as needing to build because the framework is already in the
I/flutter ( 5624): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter ( 5624): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter ( 5624): builds parent widgets before children,which means a dirty descendant will always be built.
I/flutter ( 5624): Otherwise,the framework might not visit this widget during this build phase.

这是示例代码

void main() {
    runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',theme: new ThemeData(
        priMarySwatch: Colors.blue,),home: new MyHomePage(title: 'Flutter Demo Home Page'),routes: <String,WidgetBuilder> {
        "login" : (BuildContext context) => new LoginPage(),}
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key,this.titlE}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  int _counter = 0;

    @override
    void initState() {
      super.initState();

      if(!isLoggedIn) {
        print("not logged in,going to login page");
        Navigator.of(context).pushNamed("login");
      }

    }


  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void test() {
    print("Hello");
  }

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.titlE),body: new Center(
        child: new Text(
          'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',floaTingActionButton: new FloaTingActionButton(
        onPressed: _incrementCounter,tooltip: 'Increment',child: new Icon(Icons.add),// This Trailing comma makes auto-formatTing nicer for build methods.
    );
  }

}

class LoginPage extends StatefulWidget {
  LoginPage({Key key,this.titlE}) : super(key: key);

  final String title;

  @override
  _LoginPageState createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    print("building login page");
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Sign up / Log In"),);
  }
}

我想我做错了,也许是中止小部件的构建导致了这一问题。但是,我怎么能做到这一点。

基本上:“我进入我的页面,如果未登录,请进入登录页面”

谢谢,Alexi

大佬总结

以上是大佬教程为你收集整理的Flutter重定向到initState上的页面全部内容,希望文章能够帮你解决Flutter重定向到initState上的页面所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。