【问题标题】:Why does it not work when I use pushNamedReplacement instead of pushReplacement?为什么当我使用 pushNamedReplacement 而不是 pushReplacement 时它不起作用?
【发布时间】:2019-07-22 12:52:44
【问题描述】:

我尝试使用命名路由,但它似乎不起作用,给我一个错误“找不到 _MaterialAppState 的路由生成器(“/homepage”,null)。我无法理解命名路由在此的工作案例。

 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();
                            },
                          ),
                        ));
                      }
                    },
                  ),

我需要它来使用命名路由

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    你应该使用:

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

    查看documentation了解更多详情。

    编辑:
    仅使用 1 个材料应用程序。使用两个 Material 应用(在 main.dart 和 Auth.dart 中)导致 Flutter 在最近的 MaterialApp(Auth.dart)中搜索路由,并且路由定义在最高实例(main.dart)中。
    您应该从 Auth.dart 中删除 MaterialApp

    【讨论】:

    • 感谢您的回复,但是... 'Navigator.of(context).pushReplacementNamed('/homepage'); ... 不管用。 ... I/flutter (31934): ══╡ 手势发现异常╞══════════════════════════════════ ═══════════════════════════════════ I/flutter(31934):处理a时抛出以下断言手势:I/flutter (31934):在 _WidgetsAppState 中找不到路由 RouteSettings("/homepage", null) 的生成器。
    • 尝试删除 initialRoute 参数并改用home: Auth(),。过去给我带来了麻烦。
    • 还是同样的错误……一定是小错误或者bug
    • 好的,我知道了。出现错误是因为您有两个 MaterialApp 实例。您只在第一个中定义了路由,Flutter 在第二个中搜索路由(因为更靠近呼叫)。因此,请尝试删除 MaterialApp 的第二个实例,您的问题应该会消失。
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多