I made a listview builder following a course in which I kept an elevated button(which adds video through image picker) to be index 0 and then videos shall be showed at index 0. My elevated button is not showing.
I made same code for image picker but did not happen in that case.
The listview builder is in the scaffold. There is not any error showing up in debug console
class _addVideoState extends State<addVideo> {
bool uploading = false;
double val = 0;
CollectionReference? vidRef;
firebase_storage.Reference? ref;
File? video;
VideoPlayerController? videoPlayerController;
VideoPlayerController? INvideoPlayerController;
final picker = ImagePicker();
List<File> _video = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Add video'),
actions: [
ElevatedButton(
onPressed: () {
setState(() {
uploading = true;
});
uploadFile().whenComplete(() => Navigator.of(context).pop());
},
child: const Text(
'Upload Video',
style: TextStyle(color: Colors.red),
),
)
],
),
body: ListView.builder(
itemCount: _video.length + 1,
itemBuilder: (ctx, index) {
INvideoPlayerController = VideoPlayerController.file(_video[index])
..setLooping(true)
..initialize().then((value) => INvideoPlayerController!.play());
return index == 0
? Center(
child: ElevatedButton(
onPressed: () {
!uploading ? chooseVideo() : null;
print(_video);
},
child: const Text(
'Test',
style: TextStyle(color: Colors.black),
)),
)
: AspectRatio(
aspectRatio: INvideoPlayerController!.value.aspectRatio,
child: VideoPlayer(INvideoPlayerController!),
);
}),
);
}
chooseVideo() async {
ImagePicker picker = ImagePicker();
final pickedFile = await picker.pickVideo(source: ImageSource.gallery);
setState(() {
_video.add(File(pickedFile!.path));
});
videoPlayerController = VideoPlayerController.file(File(pickedFile!.path))
..addListener(() => setState(() {
}))
..setLooping(true)
..initialize().then((_) {
setState(() {});
videoPlayerController!.play();
});
print(pickedFile.path);
print(File(pickedFile.path));
print('Kavya-choosevideo-videoPlayerController : $videoPlayerController');
if (pickedFile.path == null) retrieveLostData();
}
Future<void> retrieveLostData() async {
final LostDataResponse = await ImagePicker.platform.getLostData();
if (LostDataResponse.isEmpty) {
return;
}
if (LostDataResponse != null) {
setState(() {
_video.add(File(LostDataResponse.file!.path));
});
} else {
print(LostDataResponse.file);
}
}
Future uploadFile() async {
int i = 1;
for (var vid in _video) {
setState(() {
val = i / _video.length;
});
ref = firebase_storage.FirebaseStorage.instance
.ref()
.child('videos/${Path.basename(vid.path)}');
await ref!.putFile(vid).whenComplete(() async {
await ref!.getDownloadURL().then((value) {
vidRef!.add({
'url': value
});
i++;
});
});
}
}
@override
void initState() {
super.initState();
vidRef = FirebaseFirestore.instance.collection(
'Videourls');
INvideoPlayerController = VideoPlayerController.network('')
..addListener(() {})
..setLooping(true)
..initialize().then((value) => INvideoPlayerController!.play());
}
}
What I have tried:
I have tried Fitting all of it in a column with an elevated button above listview that is giving a lot of errors just because of nesting a listview inside a column and also some range errors