Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

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

I'm trying to pass a title to the next page. Keep in mind that FIRST and SECOND page are on two different dart file

First page:

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

Second page:

class Museo extends StatelessWidget {
    final String title;

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

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

It works but Android Studio keeps telling me this:

The named parameter 'title' isn't defined.

I tried to remove the title:

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

But it doesn't work anymore.

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);
  ^^^^^

I followed this guide: https://flutter.dev/docs/cookbook/navigation/passing-data

question from:https://stackoverflow.com/questions/65950883/flutter-send-data-to-a-new-route-saying-that-the-parameter-isnt-defined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider you have more than one parameter. Now, you will need to map each of them. So, you need to define the parameter while you're passing it. You can try this:

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

Please read more from here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...