Flutter save a network mp3 file to local directory

Viewed 3957

I am new to Flutter and Dart ,Can anyone point me in the right direction?

2 Answers
Future<dynamic> downloadFile(String url) async {
 String dir = (await getApplicationDocumentsDirectory()).path;
  File file = new File('$dir/$filename');
  var request = await http.get(url,);
  var bytes = await request.bodyBytes;//close();
  await file.writeAsBytes(bytes);
  print(file.path);
}

When you download your file you can use path_provider plugin to access device storage and save your file there.

I would recommend the Document directory which can be accessed using getApplicationDocumentsDirectory() to save your mp3 file.

Related