Search code examples
fluttermaterialpageroute

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


Solution

  • 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.