Search code examples
flutterparametersnamed

Flutter issue: Pass argument to Widget constructor as named parameter


Flutter issue. I'm trying to pass an argument to a Widget constructor as a named parameter, but I get the error: The named parameter 'uri' isn't defined. The code where I define the class is below, followed by the code where I instantiate the Widget. I'm stuck. Any help is much appreciated!

//Code defining Widget

class VideoPlayerApp extends StatelessWidget {
  VideoPlayerApp({this.uri});
  final Text uri;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Video Player Demo',
      home: VideoPlayerScreen(),
    );
  }
}

//Code defining sURI and then instantiating Widget
Text sURI = Text(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');



onPressed: () {
   Navigator.push(
      context,
      MaterialPageRoute(
         builder: (context) => VideoPlayerApp(uri: sURI),
      ),
   );
},

Solution

  • You should define uri as String, not Text.

    Try this:

    class VideoPlayerApp extends StatelessWidget {
      VideoPlayerApp({
        Key key,
        this.uri,
      }) : super(key: key);
    
      final String uri;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Video Player Demo',
          home: VideoPlayerScreen(),
        );
      }
    }