命名路线导航错误:类型'MaterialPageRoute <dynamic>'不是类型'Route <string>'的子类型

时间:2019-02-28 17:37:20

标签: flutter

我正在尝试使用命名路线导航到另一个页面:

... RaisedButton(
            child: Text('Get image'),
            onPressed: () => Navigator.pushNamed<String>(
                context, '/second'),...

但收到错误

> I/flutter (12439): ══╡ EXCEPTION CAUGHT BY GESTURE
> ╞═══════════════════════════════════════════════════════════════════
>     I/flutter (12439): The following assertion was thrown while handling a gesture:
>     I/flutter (12439): type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String>'
>     I/flutter (12439): Either the assertion indicates an error in the framework itself, or we should provide substantially
>     I/flutter (12439): more information in this error message to help you determine and fix the underlying cause.
>     I/flutter (12439): In either case, please report this assertion by filing a bug on GitHub:
>     I/flutter (12439):   https://github.com/flutter/flutter/issues/new?template=BUG.md
>     I/flutter (12439): When the exception was thrown, this was the stack:
>     I/flutter (12439): #0      NavigatorState._routeNamed package:flutter/…/widgets/navigator.dart:1426
>     I/flutter (12439): #1      NavigatorState.pushNamed package:flutter/…/widgets/navigator.dart:1473
>     I/flutter (12439): #2      Navigator.pushNamed package:flutter/…/widgets/navigator.dart:740
>     I/flutter (12439): #3      _CompleterState.build.<anonymous closure> package:my_package/completer.dart:205

该路线在首页中定义:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'XXXX',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Completer(),
      routes: {
        '/second': (BuildContext context) => SecondPage(),
      },
      onUnknownRoute: (RouteSettings settings) {
        return MaterialPageRoute(
          builder: (BuildContext context) => Completer(),
        );
      },
    );
  }
}

在此错误消息上我什么也找不到,所以我似乎产生了一个以前没有人见过的错误。

我正在慢慢修改代码以将其简化到最低限度,以找出我做错了什么,但是任何指针将不胜感激。

4 个答案:

答案 0 :(得分:3)

更改此:

     MyType result = await Navigator.pushNamed(
                context,
                MyPage.routeName,
                arguments: PageArguments(myArg);

收件人:

     var result = await Navigator.pushNamed(
                context,
                MyPage.routeName,
                arguments: PageArguments(myArg);

答案 1 :(得分:3)

发现 Navigator.pushNamed(context, '/second') as String 可以很好地处理命名路由,这在从第二个命名页面返回数据并使用更严格的类型(一切都应该如此)时很有用

答案 2 :(得分:2)

具有类型提示确实很有意义,它有助于调试并在类型不是您期望的类型时让IDE标志。 (在可能的情况下,强烈建议您打字)

您可以使用您的命名路线-Hooray!

您已经使用^l^&的{​​{1}}属性实现了路由处理,但是您发现没有地方可以添加正在创建的路由的返回类型。

routes

这意味着传递给它的任何实例都必须具有返回类型MaterialApp并为其他任何对象引发错误。

onGenerateRoute

要指定返回类型,需要花费更多的代码并使用routes: { '/second': (BuildContext context) => SecondPage(), } 的{​​{1}}属性:

dynamic

现在可以处理onGenerateRoute了-欢乐无穷!

答案 3 :(得分:1)

事后看来非常明显。

我已经在Navigator.pushNamed<String>()中添加了字符串类型提示,因为我认为我可以从调用中收到键入的值。将其删除并将其接收到var中即可解决此问题。

将其留给其他处于同一状态的人