sarbagyastha / youtube_player_flutter

A Flutter plugin for inline playback or streaming of YouTube videos using the official iFrame Player API.
https://youtube.sarbagyastha.com.np
BSD 3-Clause "New" or "Revised" License
694 stars 788 forks source link

Unhandled Exception: Bad state: Cannot add new events after calling close #864

Open joshua750 opened 1 year ago

joshua750 commented 1 year ago

when i tried close the controller in dispose i am getting this error Unhandled Exception: Bad state: Cannot add new events after calling close

import 'dart:developer';

import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:lhtv/Api/Api.dart'; import 'package:youtube_player_iframe/youtube_player_iframe.dart';

import '../Models/Slidermodel.dart';

class Livestream extends StatefulWidget {

@override State createState() => _LivestreamState();

}

class _LivestreamState extends State { late YoutubePlayerController _controller;

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

_controller = YoutubePlayerController(
  params: const YoutubePlayerParams(
    showControls: true,
    mute: false,
    showFullscreenButton: true,
    loop: false,
  ),
);

_controller.setFullScreenListener((isFullScreen) {
  if (isFullScreen) {
    // Do something when entering full-screen mode
  } else {
    // Do something when exiting full-screen mode
  }
});

}

@override void dispose() { super.dispose(); _controller.close(); print('Close'); }

@override Widget build(BuildContext context) { return SafeArea( child: Scaffold( body: FutureBuilder<Model?>( future: Api().livestream(17), builder: (BuildContext context, AsyncSnapshot<Model?> snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return Center(child: CircularProgressIndicator()); } else if (snapshot.hasError || snapshot.data == null) { return Text('Error fetching data'); } else { String videoId = snapshot.data!.nEWSAPP![0].videoId!;

          _controller = YoutubePlayerController.fromVideoId(
            videoId: '${videoId}',
            autoPlay: true,
            params: const YoutubePlayerParams(showFullscreenButton: true),
          );

          return YoutubePlayerScaffold(
            controller: _controller,
            aspectRatio: 16 / 9,
            builder: (context, player) {
              return SingleChildScrollView(
                child: Column(
                  children: [
                    player,
                    Text(
                      snapshot.data!.nEWSAPP![0].newsTitle!,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                      ),
                    ),
                    SizedBox(height: 10),
                    Text(snapshot.data!.nEWSAPP![0].newsDescription!),
                  ],
                ),
              );
            },
          );
        }
      },
    ),
  ),
);

} }

shivansh22agra commented 1 year ago

Got any solution

FaisalMohammadi commented 11 months ago

have same problem it appears just in Android for me. In iOS its working fine.

shivansh22agra commented 11 months ago

Turns out this error appear in iframe package in youtube_player_flutter

FaisalMohammadi commented 11 months ago

Their is a pull request for this issue i think merging it will solve this issue.

chillbrodev commented 1 month ago

Is it possible to get this PR merged and released? This error is prevalent in my Sentry logs.

ConnorDykes commented 1 month ago

can we please merge this?

NicolasEymael commented 3 weeks ago

@sarbagyastha any news on this?