flutterflutter-dependencies

Flutter app is crash when adding YoutubePlayer in some cases


I used youtube_player_flutter to load youtube video.But in some cases app is crash when i open the screen containing youtube video .Some time it working perfectly. I got the below exception,

[ERROR:flutter/fml/platform/android/jni_util.cc(204)] java.lang.IllegalStateException: Platform view hasn't been initialized from the platform view channel.

Code to load youtube video ,

YoutubePlayer(
                      controller: YoutubePlayerController(
                        initialVideoId: YoutubePlayer.convertUrlToId(
                                controller.reelsList[index].url.toString())
                            .toString(),
                        flags: const YoutubePlayerFlags(
                            autoPlay: false,
                            mute: false,
                            hideControls: false,
                            showLiveFullscreenButton: false),
                      ),
                      showVideoProgressIndicator: true,
                    )
 compileSdkVersion 33
 minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion

Solution

  • i got the same trouble some weeks ago when i was implementing the same feature.

    First, run on your terminal:

    flutter clean
    

    then

    flutter pub get
    

    then build your application again

    This did the work for me, By the way, i got some trouble with useHybridComposition making my app laggy on my physic device, so i've set it to false.

    I've implemented this way, but i don't really think your code have any mistakes at all;

    YoutubePlayer(
      controller: YoutubePlayerController(
        initialVideoId: videoModel.url,
        flags: const YoutubePlayerFlags(
            useHybridComposition: false,
            mute: false,
            autoPlay: false,
            disableDragSeek: true,
            loop: false,
            isLive: false,
            forceHD: false,
            enableCaption: false,
            showLiveFullscreenButton:
                false),
      ),
      showVideoProgressIndicator: true,
      progressIndicatorColor:
          colorScheme.secondary,
      progressColors: progressColors,
    ),
    

    Update me, we can discuss further if this doesn't work for you.