skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    I wrapped VideoPlayerController object with VideoPlayer widget. So that solved above error message. I can only see the images and no videos though.

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

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search