Search code examples
fluttervideo-playerflutter-video-player

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


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?


Solution

  • Use try-catch block

    try{
    _videoController.dispose();
    //Do something
    }
    catch(e){
    //Do something
    }