Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: Gridview list with both images and videos throwing an error

I have no idea what this error message means:

lib/main.dart:29:19: Error: The argument type ‘Object’ can’t be
assigned to the parameter type ‘Widget?’.
‘Object’ is from ‘dart:core’.
‘Widget’ is from ‘package:flutter/src/widgets/framework.dart’ >(‘../../flutter/packages/flutter/lib/src/widgets/framework.dart’).
? Image.network(‘https://picsum.photos/200/300’)
^
Failed to compile application.

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It seems like you are using the video_player package, so keep in mind that VideoPlayerController.network returns a controller, not a Widget and that’s why you are getting that error.

Documentation here: https://pub.dev/packages/video_player

Long story short, you need to use a VideoPlayer instead of the VideoPlayerController, something like this:

Initialize the controller:

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

using the controller to return a widget:

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

I would recommend you to read the documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading