Is there a way to check whether a VideoPlayerController from the Flutter video-player package is already disposed?

501

Use try-catch block

try{
_videoController.dispose();
//Do something
}
catch(e){
//Do something
}
Share:
501
Christian
Author by

Christian

I'm an Android programmer who studied bioinformatics at the Free University of Berlin. I wrote the proposal for https://biology.stackexchange.com/ and my current pet project is BetterEar. Small children can hear the difference between all kind of different phonemes but once they grow older they lose the ability to hear phoneme differences in foreign languages that their native language doesn't make. As a result most German native speakers can't hear the difference between belief and believe, between advice and advise or between effect and affect. My Android App trains the ability to hear the difference between different phonemes by giving the learner deliberate feedback. This allows the learner to regain the ability to distinguish phonemes that they lost as a child. While some people think that the way to improve information flow is through building brain-computer interfaces that can magically push information into brains, I think it makes sense to do our best to optimize the information flow of our existing channels. After school I went to study bioinformatics because I thought the way to improve the human brain is through looking at neurons. Today, I think improving the human brain is more about finding the right kind of stimulus. The human brain is an amazing learning machine on it's own and we don't need to put any electrodes into it, to train it to do better.

Updated on November 26, 2022

Comments

  • Christian
    Christian over 1 year

    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?

    • Huthaifa Muayyad
      Huthaifa Muayyad about 3 years
      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.
  • Christian
    Christian about 3 years
    What should I do in the catch?
  • Sourav9063
    Sourav9063 about 3 years
    Depends on your use cases. If you use this, no error will fill your logs as required by your ques.
  • gautam singh
    gautam singh almost 2 years
    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(); } ```