Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Routes 颤振重定向到initState上的页面_Routes_Dart_Flutter - Fatal编程技术网

Routes 颤振重定向到initState上的页面

Routes 颤振重定向到initState上的页面,routes,dart,flutter,Routes,Dart,Flutter,我有一个应用程序,你需要登录才能继续(例如谷歌) 我想在需要身份验证时重定向用户 但是,当我运行Navigator.of(context).pushNamed(“myroute”)时。我得到了以下错误: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 5624): The following assertion was

我有一个应用程序,你需要登录才能继续(例如谷歌)

我想在需要身份验证时重定向用户

但是,当我运行
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"),
      ),
      ),
    );
  }
}
void main(){
runApp(新的MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振演示”,
主题:新主题数据(
主样本:颜色。蓝色,
),
主页:新MyHomePage(标题:“颤振演示主页”),
路线:{
“登录”:(BuildContext上下文)=>新建登录页面(),
}
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
@凌驾
void initState(){
super.initState();
如果(!isLoggedIn){
打印(“未登录,进入登录页面”);
Navigator.of(context.pushNamed(“login”);
}
}
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
无效测试(){
打印(“你好”);
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
),
正文:新中心(
儿童:新文本(
'按钮点击$\u计数器时间${\u计数器==1?''s'}',
),
),
floatingActionButton:新的floatingActionButton(
按下时:\ u递增计数器,
工具提示:“增量”,
子:新图标(Icons.add),
),//此尾随逗号使生成方法的自动格式设置更方便。
);
}
}
类LoginPage扩展StatefulWidget{
LoginPage({Key-Key,this.title}):super(Key:Key);
最后的字符串标题;
@凌驾
_LoginPagentate createState()=>新建;
}
类_loginpagentate扩展状态{
@凌驾
小部件构建(构建上下文){
打印(“楼宇登录页面”);
归还新脚手架(
appBar:新的appBar(
标题:新文本(“注册/登录”),
),
),
);
}
}
我猜我做错了什么,可能是因为中止了小部件的构建。然而,我如何才能做到这一点

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

谢谢,
Alexi

尝试包装您的
Navigator
呼叫:

Navigator.of(context).pushNamed("login");
在通过以下方式计划的回调中:

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

import 'package:flutter/scheduler.dart';

作为另一种选择,考虑如果您可以只使用<代码> MyHomePage <代码> <代码> Bug()/Cuth>方法,如果用户未登录,则返回<代码> Login Pox<代码>,而不是<代码>脚手架< /代码>。这可能会更好地与后退按钮交互,因为您不希望用户在登录完成之前退出登录对话框。

另一种方法是在打开需要验证的新页面之前执行登录检查。 主页保留为“欢迎”/“信息”页面,当用户点击菜单项时,将执行登录检查

已登录:新页面已打开。 注销:登录页面打开

适用于我:)

您还可以:

scheduleMicrotask(() => Navigator.of(context).push(MaterialPageRoute(builder: (context) => YourComponent())));

你的第二个选择似乎比第一个好得多。我用了这个。谢谢Collin,非常有用的答案(一如既往)我在第一个解决方案中遇到了一个问题,
上下文
未定义您在哪里添加了Schulder绑定?你们从哪里得到背景???????它是undefined@b.john小部件的
状态
有一个
上下文
实例属性,您可能试图在无状态小部件或任何其他非
状态的类中调用它。另外,我建议在
initState
方法中调用它,因为我不太确定在
build
方法中调用它时可能产生的所有副作用。未定义的名称“Timer”。您从哪里导入它?@Paktalin
import-dart:async
@override
void initState() {
  super.initState();

  // it will navigate to login page as soon as this state is built
  Timer.run(() {
    Navigator.of(context).pushNamed("login");
  });
}
scheduleMicrotask(() => Navigator.of(context).push(MaterialPageRoute(builder: (context) => YourComponent())));