I want to play a video and pause it on back button but I am facing an error when i click on button this error shown Another exception was thrown: You must provide a controller to play a video I am not getting where I am missing the controller to play the video

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

class ChewieListItem extends StatefulWidget {
  VideoPlayerController videoPlayerController;
  bool looping;
  // This will contain the URL/asset path which we want to play
  @override
  _ChewieListItemState createState() => _ChewieListItemState();
}

class _ChewieListItemState extends State<ChewieListItem> {
  final videoPlayerController = VideoPlayerController.network('https://bk7l2g37dx53-hls-live.5centscdn.com/adom/5b97b7d64a64c7cd54c18b07e93570a1.sdp/index.m3u8');
  ChewieController _chewieController;

  @override
  void initState() {

    super.initState();
    // Wrapper on top of the videoPlayerController
    _chewieController = ChewieController(
      videoPlayerController: widget.videoPlayerController,
      aspectRatio: 16 / 9,
      // Prepare the video to be played and display the first frame
      autoInitialize: true,
      looping: widget.looping,
      // Errors can occur for example when trying to play a video
      // from a non-existent URL
      errorBuilder: (context, errorMessage) {
        return Center(
          child: Text(
            errorMessage,
            style: TextStyle(color: Colors.white),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    // IMPORTANT to dispose of all the used resources
    widget.videoPlayerController.dispose();
    _chewieController.dispose();
  }
}


Solution 1: Stel

widget.videoPlayerController refer to the variable inside class ChewieListItem and it's not initialized. The one you initialized is inside class _ChewieListItemState.

You can remove the variable from class ChewieListItem and use it without the widget word:

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

import 'package:video_player/video_player.dart';

class ChewieListItem extends StatefulWidget {
  bool looping;
  // This will contain the URL/asset path which we want to play
  @override
  _ChewieListItemState createState() => _ChewieListItemState();
}

class _ChewieListItemState extends State<ChewieListItem> {
  final videoPlayerController = VideoPlayerController.network('https://bk7l2g37dx53-hls-live.5centscdn.com/adom/5b97b7d64a64c7cd54c18b07e93570a1.sdp/index.m3u8');
  ChewieController _chewieController;

  @override
  void initState() {

  super.initState();
  // Wrapper on top of the videoPlayerController
    _chewieController = ChewieController(
    videoPlayerController: videoPlayerController,
    aspectRatio: 16 / 9,
    // Prepare the video to be played and display the first frame
    autoInitialize: true,
    looping: widget.looping,
    // Errors can occur for example when trying to play a video
    // from a non-existent URL
    errorBuilder: (context, errorMessage) {
      return Center(
        child: Text(
          errorMessage,
          style: TextStyle(color: Colors.white),
        ),
      );
    },
  );
}

@override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    // IMPORTANT to dispose of all the used resources
    widget.videoPlayerController.dispose();
    _chewieController.dispose();
  }
}