Skip to content

Instantly share code, notes, and snippets.

@Classy-Bear
Last active January 10, 2020 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Classy-Bear/afef129e9b878eb3202f47434c822ed4 to your computer and use it in GitHub Desktop.
Save Classy-Bear/afef129e9b878eb3202f47434c822ed4 to your computer and use it in GitHub Desktop.
Future<File> getFileFromAsset(String asset) async {
try {
var data = await rootBundle.load(asset);
var bytes = data.buffer.asUint8List();
var dir = await getApplicationDocumentsDirectory();
File file = File("${dir.path}/mi_archivo.pdf");
File assetFile = await file.writeAsBytes(bytes);
return assetFile;
} catch (e) {
throw Exception("Error al abrir el archivo");
}
}
Future<File> getFileFromUrl(String url) async {
try {
var data = await http.get(url);
var bytes = data.bodyBytes;
var dir = await getApplicationDocumentsDirectory();
File file = File("${dir.path}/mi_archivo_online.pdf");
File urlFile = await file.writeAsBytes(bytes);
return urlFile;
} catch (e) {
throw Exception("Error al abrir el archivo");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment