'I have some problems with Youtube Player Flutter

I've been trying to solve this problem by myself but I just accepted that I can't. So I'm here to ask you guys for help. I'm trying to use the Youtube Player Flutter plugin and I created a List of videos from a channel that I choose using ListTile, ListView, and FutureBuilder. What i'm trying to do is: when i click on some video i want it to play. So I'm trying to use GestureDetector, but the problem is that I don't know how to use Youtube Player Flutter as a player for onTap or Gesture Detector. I don't know if I'm doing this well and I accept suggestions of better ways to do that or a solution; One of my problems is: LateInitializationError: Field '_controller@25000317' has not been initialized. So to be honest I don't know how to use it late but when i use it that's my problem. So if I don't use late I have to change my code. from: late YoutubePlayerController _controller; to: YoutubePlayerController? _controller; and if i do that i have to use _controller! and i have the error: Null check operator used on a null value So i've tried a lot of ways to not use null check operator on a null value, but i can't. If you guys can help me with that too, I will be happy.

Here's the code


import 'package:flutter/material.dart';
import 'package:mood3/model/Video.dart';
import 'package:mood3/telas/Api.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class Videos extends StatefulWidget {

 @override
 State<Videos> createState() => _VideosState();
}

class _VideosState extends State<Videos> {
late YoutubePlayerController _controller;





 _listarVideos() {
   Api api = Api();
   return api.pesquisar("");
 }

 @override
 Widget build(BuildContext context) {


   return Container(

     padding: EdgeInsets.all(16),
     child: FutureBuilder<List<Video>?>(
       future: _listarVideos(),
       builder: (context, snapshot){
         switch(snapshot.connectionState){
           case ConnectionState.none:
           case ConnectionState.waiting:
             return Center(
               child: CircularProgressIndicator(),
             );
             break;
           case ConnectionState.active:
           case ConnectionState.done:
             if(snapshot.hasData){
               return ListView.separated(
                   itemBuilder: (context, index){

                     List<Video>? videos = snapshot.data;
                     Video video = videos![ index ];

                     return GestureDetector(
                       onTap: (){
                         void runYoutubeplayer() {
                           _controller = YoutubePlayerController(
                             initialVideoId: video.id.toString() ,
                             flags: YoutubePlayerFlags(
                               autoPlay: false,
                               mute: false,
                             ),
                           );
                         }
                         setState(() {
                           _controller.load(video.id.toString());

                         });

                       },
                       child: Column(
                         children: <Widget>[
                           Container(
                             height: 200,
                             decoration: BoxDecoration(
                                 image: DecorationImage(
                                     fit: BoxFit.cover,
                                     image: NetworkImage( video.imagem! )
                                 )
                             ),
                           ),
                           ListTile(
                             title: Text( video.titulo! ),
                             subtitle: Text( video.canal! ),
                           )
                         ],
                       ),
                     );

                   },
                   separatorBuilder: (context, index) => Divider(
                     height: 2,
                     color: Colors.grey,
                   ),
                   itemCount: snapshot.data!.length
               );
             }else{
               return Center(
                 child: Text("Nenhum dado a ser exibido!"),
               );

             }
             break;
         }
       },
     ),);
 }
}




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source