当我使用pushNamedReplacement而不是pushReplacement时,为什么它不起作用? [英] Why does it not work when I use pushNamedReplacement instead of pushReplacement?

查看:46
本文介绍了当我使用pushNamedReplacement而不是pushReplacement时,为什么它不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用命名路由,但似乎无法正常工作,给我一个错误找不到_MaterialAppState的路由生成器("/homepage",空).在此我无法理解namedRoutes的工作情况.

I tried using named routes but it doesn't seem it work giving me an error "could not find a generator for route ("/homepage",null) for _MaterialAppState.I couldn't understand the working of namedRoutes in this case.

 import 'package:flutter/material.dart';
 import './home.dart';
 import './auth.dart';

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  Widget build(BuildContext context) {
   return MaterialApp(
     initialRoute: '/',
     routes: {
       '/': (BuildContext context) => Auth(),
       '/homepage': (BuildContext context) => Home(),
      },
    );
  }
}
//somewhere in auth.dart file
    RaisedButton(
                shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25.0)),
                        color: Theme.of(context).accentColor,
                child: Column(
                      children: <Widget>[
                        Icon(Icons.arrow_forward),
                      ],
                    ),
                    onPressed: () {
                      if (_email == "email" && _pass == '123') {
                        //Navigator.pushReplacementNamed(context,'/homepage');
                        Navigator.pushReplacement(context, MaterialPageRoute(
                          builder: (BuildContext context) => Home(),
                        ));
                      } else {
                        Scaffold.of(context).showSnackBar(SnackBar(
                          content:
                              Text("Please Enter Corrent Login Details"),
                          action: SnackBarAction(
                            label: "OK",
                            onPressed: () {
                              _controllerEmail.clear();
                              _controllerPass.clear();
                            },
                          ),
                        ));
                      }
                    },
                  ),

我需要它来使用命名的路线

I need it to use the named route

推荐答案

您应使用:

Navigator.of(context).pushReplacementNamed('/homepage');

有关详细信息,请参见文档.

Look at the documentation for more details.


仅使用1个材料应用.使用两个主要应用程序(在main.dart和Auth.dart中)会导致抖动在最接近的MatertialApp(Auth.dart)中搜索路线,并且路线定义在最高instace(main.dart)中.
您应该从Auth.dart中删除MaterialApp


Use only 1 material app. Using two material apps (in main.dart and in Auth.dart) leads flutter to search the route in the closest MatertialApp (Auth.dart) and the route definition is in the highest instace (main.dart).
You should remove the MaterialApp from Auth.dart

这篇关于当我使用pushNamedReplacement而不是pushReplacement时,为什么它不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆