Saya mencoba mendapatkan tautan video youtube untuk paket Youtube_player_flutter yang ditulis dalam file VideoCard.dart saya. Sekarang saya meneruskan tautan video sebagai parameter ke konstruktor yang saya buat tetapi saya mendapatkan The instance member 'widget' can't be accessed in an initializer.

Saya pikir masalahnya adalah dengan menggunakan widget stateless tetapi bahkan setelah mengubahnya menjadi stateful, masalahnya tetap ada.

class _VideoCardState extends State<VideoCard> {
  // static String videoID = 'PQSagzssvUQ';
  String videoId = widget.url;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    videoId = widget.url;
  }

  YoutubePlayerController _controller = YoutubePlayerController(
    initialVideoId: videoId,
    flags: YoutubePlayerFlags(
      autoPlay: true,
      mute: false,
    ),
  );
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: Container(
        decoration: BoxDecoration(
          color: Color(0xffff8f8f8),
          borderRadius: BorderRadius.circular(15),
        ),
        child: YoutubePlayer(
          controller: _controller,
          liveUIColor: Colors.amber,
          showVideoProgressIndicator: true,
        ),
      ),
    );
  }
}

Pembaruan 1:

class _VideoCardState extends State<VideoCard> {
  String get videoId => widget.url;

  // videoId = YoutubePlayer.convertUrlToId(
  //         "https://www.youtube.com/watch?v=S9bCLPwzSC0")
  //     .toString();
  @override
  void initState() {
    super.initState();
    // print(widget.url);
    videoId = widget.url;
  }

  YoutubePlayerController _controller = YoutubePlayerController(
    initialVideoId: videoId, ------------------------> The instance member 'videoId' can't be accessed in an initializer.
    flags: YoutubePlayerFlags(
      autoPlay: true,
      mute: false,
    ),
  );
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: Container(
        decoration: BoxDecoration(
          color: Color(0xffff8f8f8),
          borderRadius: BorderRadius.circular(15),
        ),
        child: YoutubePlayer(
          controller: _controller,
          liveUIColor: Colors.amber,
          showVideoProgressIndicator: true,
        ),
      ),
    );
  }
}
-1
Fahim Hoque 28 April 2021, 21:45

2 jawaban

Jawaban Terbaik

Inilah yang berhasil untuk saya:

class _VideoCardState extends State<VideoCard> {
  late String videoId;
  late YoutubePlayerController _controller;

  @override
  void initState() {
    super.initState();
    videoId = YoutubePlayer.convertUrlToId(widget.url).toString();

    _controller = YoutubePlayerController(
      initialVideoId: videoId,
      flags: YoutubePlayerFlags(
        autoPlay: false,
        mute: false,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: Container(
        decoration: BoxDecoration(
          color: Color(0xffff8f8f8),
          borderRadius: BorderRadius.circular(15),
        ),
        child: YoutubePlayer(
          controller: _controller,
          liveUIColor: Colors.amber,
          showVideoProgressIndicator: true,
        ),
      ),
    );
  }
}
0
Fahim Hoque 29 April 2021, 20:09

Anda dapat mencoba menggunakan getter, seperti pada kode berikut :

class _VideoCardState extends State<VideoCard> {
  String get videoId => widget.url;

  YoutubePlayerController get _controller => YoutubePlayerController(
    initialVideoId: videoId,
    flags: YoutubePlayerFlags(
      autoPlay: true,
      mute: false,
    ),
  );

  ...

}
0
ישו אוהב אותך 29 April 2021, 05:41