Skip to content

Instantly share code, notes, and snippets.

@Gi972
Forked from slightfoot/download.dart
Created June 20, 2019 23:16
Show Gist options
  • Select an option

  • Save Gi972/a82d56b08df34e29b26999cbb0b1fc82 to your computer and use it in GitHub Desktop.

Select an option

Save Gi972/a82d56b08df34e29b26999cbb0b1fc82 to your computer and use it in GitHub Desktop.
Download file in Dart/Flutter
static var httpClient = new HttpClient();
Future<File> _downloadFile(String url, String filename) async {
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = new File('$dir/$filename');
await file.writeAsBytes(bytes);
return file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment