fluttercommunity / fluttercommunity/chewie

Controller not disposing when navigating away

Open
#233 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
2.1k
Forks
1.1k
Avg merge
6d 8h
Merged PRs (30d)
6

Description

Hi,

when I pop off the videoplayer screen, it seems neither the videoplayer controller or the chewie controller are being disposed (I can still hear the video playing from the new screen).

Here is my code:

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

class AddVideoScreen extends StatefulWidget {
static const routeName = '/add-video';
@override
_AddVideoScreenState createState() => _AddVideoScreenState();
}

class _AddVideoScreenState extends State {
final _titleController = TextEditingController();
VideoPlayerController _videoPlayerController;
ChewieController _chewieController;
Future _initializeVideoPlayerFuture;
String videoPath;

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

@override
void didChangeDependencies() {
super.didChangeDependencies();
videoPath = ModalRoute.of(context).settings.arguments as String;

_videoPlayerController = VideoPlayerController.file(File(videoPath));

_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
aspectRatio: 2 / 3,
autoPlay: true,
looping: true,

);
}

@override
void dispose() {
_videoPlayerController.dispose();
_videoPlayerController = null;
_chewieController.dispose();
_chewieController = null;
super.dispose();
}

@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
leading: Container(),
title: Text('Add a New Video'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextField(
decoration: InputDecoration(labelText: 'Title'),
controller: _titleController,
),
SizedBox(
height: 10.0,
),
Center(
child: Chewie(
controller: _chewieController,

),
),
FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return AspectRatio(
aspectRatio: _videoPlayerController.value.aspectRatio,
// Use the VideoPlayer widget to display the video.
child: Chewie(
controller: _chewieController,
),
);
} else {
return Container();
// If the VideoPlayerController is still initializing, show a
// loading spinner.
//return Center(child: CircularProgressIndicator());
}
},
),
RaisedButton.icon(
icon: Icon(Icons.delete),
label: Text('Delete Video'),
elevation: 0,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Theme.of(context).errorColor,
onPressed: () {

Navigator.of(context).pop();
},
),
RaisedButton.icon(
icon: Icon(Icons.add),
label: Text('Add Video'),
elevation: 0,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Theme.of(context).accentColor,
onPressed: () {},
),
],
),
),
),

),
);
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.