I am using youtubePlayer for showing yotube videos in my app but i got error like "LateInitializationError: Field '_youtubePlayerController@341268173' has not been initialized."
Following are my code:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:mahuva_azadari/Models/LinkModel.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
import 'package:http/http.dart' as http;
import '../Models/Hexa color.dart';
class LiveProgram extends StatefulWidget {
const LiveProgram({Key? key}) : super(key: key);
@override
State<LiveProgram> createState() => _LiveProgramState();
}
class _LiveProgramState extends State<LiveProgram> {
late YoutubePlayerController _youtubePlayerController;
Future<LinkModel> getAllLinks() async {
var url = "https://aeliya.000webhostapp.com/MajlisLinks.php";
var response = await http.get(Uri.parse(url));
var jsondata = jsonDecode(response.body.toString());
if (response.statusCode == 200) {
return LinkModel.fromJson(jsondata);
} else {
return LinkModel.fromJson(jsondata);
}
}
@override
void deactive() {
_youtubePlayerController.pause();
super.deactivate();
print("deactivate");
}
@override
void dipose() {
_youtubePlayerController.dispose();
print("dispose");
super.dispose();
}
@override
Widget build(BuildContext context) {
return YoutubePlayerBuilder(
player: YoutubePlayer(
controller: _youtubePlayerController,
),
builder: (context,player){
return SafeArea(
child: Scaffold(
appBar: AppBar(title: Text("Live Program",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25
),),
centerTitle: true,
backgroundColor: Color(hexColors('006064')),
),
body: FutureBuilder<LinkModel>(
future: getAllLinks(),
builder: (context, snapshot){
if(snapshot.hasData){
return ListView.builder(
itemCount: snapshot.data!.data!.length,
itemBuilder: (context, index){
var allItem = snapshot.data!.data![index];
return Container(
margin: EdgeInsets.all(8),
child: Card(
color: Color(hexColors("00BCD4")),
elevation: 6,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(10.0)),
child: Column(
children: [
YoutubePlayer(
controller: _youtubePlayerController = YoutubePlayerController(
initialVideoId: YoutubePlayer.convertUrlToId(allItem.link!).toString(),
flags: YoutubePlayerFlags(
autoPlay: false,
),
)..addListener(() {
if(mounted){
setState(() {});
}
}),
showVideoProgressIndicator: true,
),
SizedBox(height: 10,),
Text(_youtubePlayerController.metadata.title.isEmpty? 'Loading Title.....' :
"Title: " _youtubePlayerController.metadata.title,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w700,
fontStyle: FontStyle.italic),
),
Text("City : ${allItem.city!}"),
Text("City : ${allItem.postDateTime!}")
],
),
),
);
});
}else{
return Center(child: CircularProgressIndicator(),);
}
},
),
),
);
},
);
}
}
change in variable initialization :-
YoutubePlayerController? _youtubePlayerController;
I tried to solve this issue removing "late" and write "?" , then it's breaking all my code. can any one please help me for solving this issue? Thanks in avdvance.
CodePudding user response:
You just haven't instantiated your _youtubePlayerController
.
As your widget is a Stateful Widget
, you should add an initState()
method:
@override
void initState() {
_youtubePlayerController = YoutubePlayerController();
super.initState();
}