I'm working on a project in flutter and I need to implement video downloading from server feature .I'm considering using Dio library and saving the downloaded video to getApplicationDocumentsDirectory()
but I haven't find an example of what I want to achieve , I tried some examples and tried to modify them but it just did not work .
Anyone has an idea on how to manage that ? Thanks .
Asked
Active
Viewed 9,928 times
3

Sajjad Emad
- 53
- 1
- 4
1 Answers
9
You can copy paste run full code below
code snippet
Future<void> downloadFile() async {
Dio dio = Dio();
try {
var dir = await getApplicationDocumentsDirectory();
print("path ${dir.path}");
await dio.download(imgUrl, "${dir.path}/demo.mp4",
onReceiveProgress: (rec, total) {
print("Rec: $rec , Total: $total");
setState(() {
downloading = true;
progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
});
});
} catch (e) {
print(e);
}
setState(() {
downloading = false;
progressString = "Completed";
});
print("Download completed");
}
working demo
output
I/flutter (18244): path /data/user/0/yourdoamin.yourproject/app_flutter
I/flutter (18244): Rec: 2124 , Total: 2429896
I/flutter (18244): Rec: 4883 , Total: 2429896
...
I/flutter (18244): Rec: 2429896 , Total: 2429896
I/flutter (18244): Download completed
full code
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
void main() => runApp(MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
));
class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return new MyAppState();
}
}
class MyAppState extends State<MyApp> {
final imgUrl = "https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";
bool downloading = false;
var progressString = "";
@override
void initState() {
super.initState();
downloadFile();
}
Future<void> downloadFile() async {
Dio dio = Dio();
try {
var dir = await getApplicationDocumentsDirectory();
print("path ${dir.path}");
await dio.download(imgUrl, "${dir.path}/demo.mp4",
onReceiveProgress: (rec, total) {
print("Rec: $rec , Total: $total");
setState(() {
downloading = true;
progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
});
});
} catch (e) {
print(e);
}
setState(() {
downloading = false;
progressString = "Completed";
});
print("Download completed");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("AppBar"),
),
body: Center(
child: downloading
? Container(
height: 120.0,
width: 200.0,
child: Card(
color: Colors.black,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
SizedBox(
height: 20.0,
),
Text(
"Downloading File: $progressString",
style: TextStyle(
color: Colors.white,
),
)
],
),
),
)
: Text("No Data"),
),
);
}
}

chunhunghan
- 51,087
- 5
- 102
- 120
-
1I spend a lot of time finding that directory but not able to find it. I'm downloading videos. – Abdullah Khan Jul 30 '20 at 16:48
-
2In order to download file to the downloads folder in android you have to request the permission for storage which is specified first in the android manifest and then write to /storage/emulated/0/Download. The methods provided by path provider only provide locations to the apps local directory. – Rohit Singh Aug 09 '20 at 09:19
-
@chunhunghan what will happen when download fails completing for internet connection and how can we find that file to play later ? – Mijanur Rahaman Aug 28 '21 at 04:07
-
if I change the link to a youtube video it doesn't work, any advice> – Steve Feb 04 '23 at 18:55