im try some code to get video from database mysql with flutter. but i have problem with the 'chewiecontroller' has not been initialized i had try to change the late variable with 'ChewieController? chewieController' but still error. any solution for that problem?
This is my current code:
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class PlayVideoChewie extends StatefulWidget {
final String url;
const PlayVideoChewie({required this.url});
@override
State<PlayVideoChewie> createState() => _PlayVideoChewieState();
}
class _PlayVideoChewieState extends State<PlayVideoChewie> {
late VideoPlayerController videoPlayerController;
late ChewieController chewieController;
Future initializeVideo() async {
videoPlayerController = VideoPlayerController.network(widget.url);
await videoPlayerController.initialize();
chewieController = ChewieController(
videoPlayerController: videoPlayerController,
autoPlay: true,
looping: true,
);
setState(() { });
}
@override
void initState() {
initializeVideo();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Playvid"),
),
body: Container(
height: 250,
child: chewieController != null &&
chewieController.videoPlayerController.value.isInitialized
? Chewie(
controller: chewieController,
)
: Container(
child: CircularProgressIndicator(),
),
),
);
}
@override
void dispose() {
videoPlayerController.dispose();
chewieController.dispose();
super.dispose();
}
}
any help will be valuable for me. thank you.
CodePudding user response:
because you have to
await videoPlayerController.initialize();
chewieController is only initalize after that, so it cause error. Change it like this :
class _PlayVideoChewieState extends State<PlayVideoChewie> {
late VideoPlayerController videoPlayerController;
ChewieController? chewieController;
Future initializeVideo() async {
videoPlayerController = VideoPlayerController.network(widget.url);
await videoPlayerController.initialize();
chewieController = ChewieController(
videoPlayerController: videoPlayerController,
autoPlay: true,
looping: true,
);
setState(() { });
}
@override
void initState() {
initializeVideo();
super.initState();
}
@override
Widget build(BuildContext context) {
if (chewieController == null) {
return Container(
child: CircularProgressIndicator(),
);
}
return Scaffold(
appBar: AppBar(
title: Text("Playvid"),
),
body: Container(
height: 250,
child: Chewie(
controller: chewieController!,
),
),
);
}
@override
void dispose() {
videoPlayerController.dispose();
chewieController?.dispose();
super.dispose();
}
}
CodePudding user response:
Did you try to declare both variables like that?
ChewieController? chewieController;
VideoPlayerController? videoPlayerController;
CodePudding user response:
This is a common problem when you call a Future
from initState
, if the result of it is needed for the first build. The initState
function is not async, so you can't use await
to wait for the result of initializeVideo()
. So what happens is that the build
method will run earlier than chewieController
is initalized.
The proper solution is to implement a FutureBuilder
in the build
method.
You will get the lateInitalization
error even if you try to check with the below line, because with the late
keyword it will throw an error if you read a variable that has not been set:
if (chewieController == null)
Refer to the documentation on how to create a FutureBuilder
.