dart Flutter:包含图像和视频的Gridview列表抛出错误

j9per5c4  于 2023-04-09  发布在  Flutter
关注(0)|答案(2)|浏览(116)

我不知道这个错误消息是什么意思:
lib/main.dart:29:19:错误:无法将参数类型“Object”分配给参数类型“Widget?”。“Object”来自“dart:core”。“Widget”来自“package:flutter/src/widgets/framework.dart”〉('../../flutter/packages/flutter/lib/src/widgets/framework. dart')。?Image.network('https://picsum.photos/200/300')^无法编译应用程序。

body: GridView.count(
          // Create a grid with 2 columns. If you change the scrollDirection to
          // horizontal, this produces 2 rows.
          crossAxisCount: 2,
          // Generate 100 widgets that display their index in the List.
          children: List.generate(100, (index) {
            return Center(
              child: index % 2 == 0
                  ? Image.network('https://picsum.photos/200/300')
                  : VideoPlayerController.network('https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
              ),
            );
          }),
        ),
igsr9ssn

igsr9ssn1#

看起来你正在使用video_player包,所以请记住VideoPlayerController.network返回的是一个控制器,而不是Widget,这就是为什么你会得到这个错误。
此处的文档:https://pub.dev/packages/video_player
长话短说,你需要使用VideoPlayer而不是VideoPlayerController,类似于这样:
初始化控制器:

late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4')
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });
  }

使用控制器返回一个widget:

AspectRatio(
       aspectRatio: _controller.value.aspectRatio,
       child: VideoPlayer(_controller),
   )

我建议您阅读文档

ndh0cuux

ndh0cuux2#

我用VideoPlayer小部件 Package 了VideoPlayerController对象。所以解决了上面的错误消息。我只能看到图像,没有视频。

child: index % 2 == 0
               ? Image.network('https://picsum.photos/200/300')
               : VideoPlayer(VideoPlayerController.network('https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
           ),),

         );

相关问题