i got red screen when the fvideo is empty
late YoutubePlayerController? _controller;
@override
void initState() {
super.initState();
final videoID = YoutubePlayer.convertUrlToId(widget.fvideo);
setState(() {
_controller = YoutubePlayerController(
initialVideoId: videoID!,
flags: const YoutubePlayerFlags(
autoPlay: false,
),
);
});
}
@override
void dispose() {
_controller?.dispose(); // Dispose the controller only if it's not null
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
if (widget.fvideo.isNotEmpty && _controller != null)
YoutubePlayer(
controller: _controller!,
showVideoProgressIndicator: true,
bottomActions: [
CurrentPosition(),
ProgressBar(isExpanded: true),
],
)
else
Text('No video provided'), // Display a message if no video URL is provided
],
i try to add if statment in the controller but i go controller error , when there is a video the code run success, when it is empty i got the problem enter image description here
I suspect that your videoID is null, thats why you are getting the "null check operator used on a null value" error. You should check if the videoID is null or not, using the if statement or ternary conditional operator (if you have some default videoId to set).
@override
void initState() {
super.initState();
final videoID = YoutubePlayer.convertUrlToId(widget.fvideo);
// Check if videoID is not null before creating the controller
if (videoID != null) {
setState(() {
_controller = YoutubePlayerController(
initialVideoId: videoID,
flags: const YoutubePlayerFlags(
autoPlay: false,
),
);
});
} else {
print('Invalid video URL: ${widget.fvideo}');
}
}