简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM