2

At a point in my code I now that I won't need a VideoPlayerController anymore but I'm not sure whether or not a VideoPlayerController is already disposed. Currently, I call the dispose() method but when the VideoPlayerController is already disposed that throws the error:

2021-04-08 23:35:07.602 1898-2090/com.learningleaflets.anatomyleaflet E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: A VideoPlayerController was used after being disposed.
    Once you have called dispose() on a VideoPlayerController, it can no longer be used.
    #0      ChangeNotifier._debugAssertNotDisposed.<anonymous closure> (package:flutter/src/foundation/change_notifier.dart:117:9)
    #1      ChangeNotifier._debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:123:6)
    #2      ChangeNotifier.dispose (package:flutter/src/foundation/change_notifier.dart:212:12)
    #3      VideoPlayerController.dispose (package:video_player/video_player.dart:383:11)
    <asynchronous suspension>

I don't want that error to fill my logs, so is there a way to check whether a VideoPlayerController is already disposed to avoiding calling dispose() on it?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • I don't think this is the cause of the error being displayed. The error means that you tried to use the `VideoPlayerController` after it was disposed. Could be done by mistake also if more than one video is using the same controller. You dispose of it in the first video, then try to use the same one to play the second video, then this error is thrown. – Huthaifa Muayyad Apr 08 '21 at 22:13

1 Answers1

3

Use try-catch block

try{
_videoController.dispose();
//Do something
}
catch(e){
//Do something
}
Sourav9063
  • 292
  • 2
  • 7
  • What should I do in the catch? – Christian Apr 09 '21 at 09:14
  • 1
    Depends on your use cases. If you use this, no error will fill your logs as required by your ques. – Sourav9063 Apr 09 '21 at 18:05
  • its not catched in the dispose method ``` try { videoNotifier.videoPlayerController .removeListener(videoNotifier.listener); videoNotifier.videoPlayerController.dispose(); } catch (e) { log('Err : dispose custom controller $e'); } finally { super.dispose(); } ``` – gautam singh rathore May 17 '22 at 12:09