1

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

1 Answers1

2

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.

Akif
  • 7,098
  • 7
  • 27
  • 53
  • 1
    Can you provide a screenshot of the error? – Akif Jan 29 '21 at 08:27
  • 1
    Did you try to add "const" here: const Museo({Key key, @required this.title}) : super(key: key); – Akif Jan 29 '21 at 08:29
  • 1
    And, if it works but Android Studio keeps telling the error, you can try "invalidate caches/restart". – Akif Jan 29 '21 at 08:31
  • I tried to add const but still nothing. Console is not giving me any error, but AndroidStudio continue to mark "title" and says "The named parameter 'title' isn't defined." I forgot to say that first page and second page are on two different dart file – PercivallAkihiko Jan 29 '21 at 08:35
  • 1
    Please add a screenshot of the mark of the Android Studio. Before that, restart the Android Studio. I hope, it will be fixed. – Akif Jan 29 '21 at 08:36
  • Restart Android Studio worked! I removed const and still working, thanks – PercivallAkihiko Jan 29 '21 at 08:42