找不到用于路线的发电机 [英] Could not find a generator for route

查看:73
本文介绍了找不到用于路线的发电机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,会在Flutter上发表关于路线和分页器的一个例外.

I´m newbie to flutter and reveice one exception about route and paginator in Flutter.

EXCEPTION CAUGHT BY GESTURE
The following assertion was thrown while handling a gesture:
Could not find a generator for route "/listadecompras" in the _MaterialAppState.

遵循代码摘录:

import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
// ...                    
                return new ListTile(
                  onTap: () {                                         
                    Navigator.pushNamed(context, "/listadecompras");
                  },
// ...
}


class ListaDeCompras extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
// ...
}
}


void main() {
runApp(new MaterialApp(
    home: new MyApp(), 
    routes: <String, WidgetBuilder>{
        "/listadecompras": (BuildContext context) => new ListaDeCompras()
    }
));
}

请,有人可以给我一些建议吗? 预先感谢您的关注

Please, someone could send some advice? thanks in advance for your attention

推荐答案

这是因为您已实例化了两个MaterialApp小部件.您需要删除MyApp类中的一个,并可能将其更改为Scaffold,如下所示:

This is because you have instantiated two MaterialApp widgets. You need to remove the one in MyApp class and may change it to Scaffold instead like this:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
// ...                    
                return new ListTile(
                  onTap: () {                                         
                    Navigator.pushNamed(context, "/listadecompras");
                  },
// ...
}

您的代码中发生的事情是该路由正在尝试解析最接近的MaterialApp,它没有路由定义.也就是说,应该仅使用一个MaterialApp作为窗口小部件树的根.

What happened in your code is the route is trying to resolve for the nearest MaterialApp which has no route definition. That said you should use only one MaterialApp as the root of your widget tree.

这篇关于找不到用于路线的发电机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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