skip to Main Content

I’m attempting to create a page in my app which renders a video background, using video_player. I want to pass a video file into my video player widget, and I have followed the docs and this article for passing in a value, but I still have the following issues:

  1. If I follow the article where I call VideoPlayer(_controller) at the bottom of video_player.dart, I get the error that video is required so I must include it
  2. If I include video as VideoPlayer(_controller, video:widget.video) , then I get Too many positional arguments: 0 expected, but 1 found. over _controller.
  3. If I leave out _controller, then it doesn’t get initialised and the app fails.
  4. I can’t remove required from video, otherwise it throws The parameter 'video' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

How can I correctly pass video_player a file without all these errors throwing?

Here are my two files

video_player.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPlayer extends StatefulWidget {
  final File video;

  VideoPlayer({
    super.key, 
    required this.video
  });

  @override
  _VideoPlayerState createState() => _VideoPlayerState();
}

class _VideoPlayerState extends State<VideoPlayer> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();

    _controller = VideoPlayerController.file(widget.video);

    _controller.addListener(() {
      setState(() {});
    });
    _controller.setLooping(true);
    _controller.initialize().then((_) => setState(() {}));
    _controller.play();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return FittedBox(
      fit: BoxFit.fill,
        child: _controller.value.isInitialized 
        ? SizedBox(
          width: _controller.value.size.width,
          height: _controller.value.size.height,
          // The problem child
          child: VideoPlayer(_controller, video:widget.video)) 
        : Container(),
    );
  }
}

page.dart

class MyPage extends StatelessWidget {
  const MyPage({super.key, required this.file});

  final File file;

  @override
  Widget build(BuildContext context) {
    return Stack(
          fit: StackFit.expand, 
          children: [
            VideoPlayer(video:File(file)),
            PageContent(),
        ]);
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    Looks like the issue was in my naming convention. By calling the Widget itself VideoPlayer, whenever I call the flutter package VideoPlayer(_controller), it was assuming I meant the widget I declared instead of from the package.

    Correct call: child: VideoPlayer(_controller)

    Then renamed the Widget I've created to something else!


  2. You’re meant to be calling video_player, not VideoPlayer

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