Flutter 将数据发送到新路由,表示参数未定义

Flutter send data to a new route saying that the parameter isn't defined

我正在尝试将标题传递到下一页。 请记住,第一页和第二页位于两个不同的飞镖文件中

首页:

Navigator.push(
    context,
    MaterialPageRoute(
        builder: (context) => Museo(title: newMuseo[index]['title'])
    )
);

第二页:

class Museo extends StatelessWidget {
    final String title;

    Museo({Key key, @required this.title}) : super(key: key);

    Widget build(BuildContext context){
        ...
    }
}

它有效,但 Android Studio 一直告诉我:

The named parameter 'title' isn't defined.

我试图删除标题:

Navigator.push(
    context,
    MaterialPageRoute(
        builder: (context) => Museo(newMuseo[index]['title'])
    )
);

但它不再起作用了。

lib/Home.dart:110:70: Error: Too many positional arguments: 0 allowed, but 1 found.
Try removing the extra positional arguments.
                                          builder: (context) => Museo(newMuseo[index]['title'])
                                                                     ^
lib/Museo.dart:294:3: Context: Found this candidate, but the arguments don't match.
  Museo({Key key, @required this.title}) : super(key: key);
  ^^^^^

我遵循了这个指南: https://flutter.dev/docs/cookbook/navigation/passing-data

考虑到您有多个参数。现在,您需要映射它们中的每一个。因此,您需要在传递参数时定义它。你可以试试这个:

Navigator.push(
    context,
    MaterialPageRoute(
        builder: (context) => Museo(title: newMuseo[index]['title'])
    )
);

请从 here 阅读更多内容。