Flutter 问题:将参数作为命名参数传递给 Widget 构造函数

Flutter issue: Pass argument to Widget constructor as named parameter

颤动问题。我试图将参数作为命名参数传递给 Widget 构造函数,但我收到错误消息:未定义命名参数 'uri'。下面是我定义 class 的代码,然后是我实例化 Widget 的代码。我卡住了。非常感谢任何帮助!

//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),
      ),
   );
},

您应该将 uri 定义为 String,而不是 Text

试试这个:

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(),
    );
  }
}