As there are not many articles downloaded from the shutter breakpoint, and there are few cases, I found them on the DIO case after searching for them for a long time, and it took me some time to be wronged
based on DIO: ^ 4.0.0
and Path_ Provider: ^ 2.0.2
implements the function of downloading a large file
Core code
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
class DownloadFile {
/// Used to record the url being downloaded to avoid duplicate downloads
static var downloadingUrls = Map<String, CancelToken>();
/// Breakpoint downloading of large files
static Future<void> download({
required String url,
required String savePath,
ProgressCallback?onReceiveProgress,
void Function()?done,
void Function(DioError)?failed,
}) async {
int downloadStart = 0;
bool fileExists = false;
File f = File(savePath);
if (await f.exists()) {
downloadStart = f.lengthSync();
fileExists = true;
}
print("start: $downloadStart");
if (fileExists && downloadingUrls.containsKey(url)) {
return;
}
var dio = Dio();
CancelToken cancelToken = CancelToken();
downloadingUrls[url] = cancelToken;
try {
var response = await dio.get<ResponseBody>(
url,
options: Options(
/// Receive response data as a stream
responseType: ResponseType.stream,
followRedirects: false,
headers: {
/// Downloading key locations in segments
"range": "bytes=$downloadStart-",
},
),
);
File file = File(savePath);
RandomAccessFile raf = file.openSync(mode: FileMode.append);
int received = downloadStart;
int total = await _getContentLength(response);
Stream<Uint8List> stream = response.data!.stream;
StreamSubscription<Uint8List>?subscription;
subscription = stream.listen(
(data) {
/// Write files must be synchronized
raf.writeFromSync(data);
received += data.length;
onReceiveProgress?.call(received, total);
},
onDone: () async {
downloadingUrls.remove(url);
await raf.close();
done?.call();
},
onError: (e) async {
await raf.close();
downloadingUrls.remove(url);
failed?.call(e as DioError);
},
cancelOnError: true,
);
cancelToken.whenCancel.then((_) async {
await subscription?.cancel();
await raf.close();
});
} on DioError catch (error) {
/// The request has been sent and the server responds with a status code that it is not in the range of 200
if (CancelToken.isCancel(error)) {
print("Download cancelled");
} else {
failed?.call(error);
}
downloadingUrls.remove(url);
}
}
/// Get the size of the downloaded file
static Future<int> _getContentLength(Response<ResponseBody> response) async {
try {
var headerContent =
response.headers.value(HttpHeaders.contentRangeHeader);
print("download files: $headerContent");
if (headerContent != null) {
return int.parse(headerContent.split('/').last);
} else {
return 0;
}
} catch (e) {
return 0;
}
}
/// Cancel Mission
static void cancelDownload(String url) {
downloadingUrls[url]?.cancel();
downloadingUrls.remove(url);
}
}
Call case
void main() {
runApp(TestMyApp());
}
class TestMyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _TestMyAppState();
}
}
class _TestMyAppState extends State<TestMyApp> {
void download() async {
var url = "mp4";
Directory dir = await getApplicationDocumentsDirectory();
var sDCardDir = dir.path;
var savePath = sDCardDir + "/video/1.mp4";
File f = File(sDCardDir + "/video");
if (!await f.exists()) {
new Directory(sDCardDir + "/video").createSync();
}
await DownLoadManage().download(
url: url,
savePath: savePath,
onReceiveProgress: (received, total) {
if (total != -1) {
print("Download1Received:" +
received.toString() +
"Total:" +
total.toString() +
"Progress.+${(received/total * 100).floor()}%");
}
},
done: () {
print("Download 1 completed");
},
failed: (e) {
print("Download 1 failed:" + e.toString());
},
);
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Center(
child: GestureDetector(
onTap: () {
download();
},
child: Container(
width: 150,
height: 150,
color: Colors.red,
),
),
),
);
}
}
Read More:
- Realization of springboot authorization verification technology based on JWT
- Router DIO network request: dioerror[ DioErrorType.RESPONSE ]: HTTP status error [400] or [500]
- Bug: unable to download source code in idea, error cannot download sources sources not found for:XXX
- Matlab realization of user-defined curve and legend
- Abnormal crash of APP startup — pointer being free was not allocated * * set a breakpoint in malloc_ error_ break to debug
- Keil prompts the stlink download program to prompt error: Flash download failed – target DLL has been cancelled
- [flutter] report error with Dio[ DioErrorType.DEFAULT ]: SocketException: OS Error: Connection refused, errno =11
- Expanding the file system based on XFS in LVM
- Solution to the problem that keil can’t set breakpoint
- Implementation of HTTPS file server based on nginx in win2012
- How to install IDM Extension in Chromium-based Microsoft Edge (Canary/Dev)
- Clion breakpoint not triggered debugging no response to solve the problem
- Installing xgboost in anaconda is based on win10 32 / 64 bit system
- NPM modify download dependency (modify global download and cache path)
- Under idea2020.3 struts.xml The solution of “red report” based on extensions = “struts default” in
- Android is Stuck in Download maven-metadata.xml [How to Solve]
- Solution: vs currently does not hit the breakpoint and has not loaded any symbols for the document
- Due to multi process — pychar debug breakpoint debugging encounter pychar dataloader will be stuck
- Go get github.com/kotakanbe/go -Problems encountered in CVE dictionary Download
- Installation and use of OpenGL based on CodeBlocks