Hello everyone,
i am trying to perform download task from a firebase url.but it stucks, once i click on the download icon it shows the progress bar with 100% directly and nothing else done the app state is not updating and the flag is not being set .appreciate if you can help!
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart';
class Download extends StatefulWidget {
const Download({
Key? key,
this.width,
this.height,
required this.audiourl,
required this.name,
required this.size,
required this.duration,
this.docRef,
}) : super(key: key);
final double? width;
final double? height;
final String audiourl;
final String name;
final double size;
final String duration;
final DocumentReference? docRef;
@override
_DownloadState createState() => _DownloadState();
}
class _DownloadState extends State<Download> {
int flag = 0; // 0: Initial state, 1: Downloading, 2: Download complete
double progress = 0.0;
Future<void> dwonloadAudioSession(String Audiourl, String name) async {
try {
setState(() {
this.flag = 1;
});
final ref = FirebaseStorage.instance.ref().child(Audiourl);
// Get the application document directory
Directory appDocDir = await getApplicationDocumentsDirectory();
File downloadToFile = File('${appDocDir.path}/$name.mp3');
// Start the download
final downloadTask = ref.writeToFile(downloadToFile);
// Listen for progress
await downloadTask.snapshotEvents.listen((taskSnapshot) async {
switch (taskSnapshot.state) {
case TaskState.running:
setState(() {
this.progress =
(taskSnapshot.bytesTransferred / taskSnapshot.totalBytes);
});
print("Download is $progress% complete.");
break;
case TaskState.paused:
print("Download paused.");
break;
case TaskState.success:
print("Download complete.");
setState(() {
this.flag = 2;
FFAppState().addToDownloaded(DownloadedStruct(
name: widget.name,
size: widget.size,
data: '${appDocDir.path}/$name.mp3',
duration: widget.duration,
docRef: widget.docRef));
});
break;
case TaskState.canceled:
print("Download canceled.");
setState(() {
this.flag = 0;
});
break;
case TaskState.error:
print('task error');
setState(() {
this.flag = 0;
});
break;
}
});
} catch (e) {
print('Error occurred while downloading file: $e');
setState(() {
flag = 0;
});
}
}
@override
Widget build(BuildContext context) {
return Center(
child: Stack(
alignment: Alignment.center,
children: [
// Show the download icon if flag is 0
if (flag == 0)
IconButton(
icon: Icon(Icons.download),
alignment: Alignment.center,
onPressed: () async {
// Start downloading
await dwonloadAudioSession(widget.audiourl, widget.name);
},
),
// Show the progress indicator if flag is 1
if (flag == 1)
CircularProgressIndicator(
value: this.progress, // Show half progress
backgroundColor: Colors.red,
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
strokeWidth: 5.0,
semanticsLabel: 'Downloading',
semanticsValue: '${this.progress}%', // Value for accessibility
),
// Show the check mark icon if flag is 2
if (flag == 2)
Icon(
Icons.check_circle,
color: Colors.green,
size: 48.0,
),
],
),
);
}
}