Last active
January 10, 2020 22:13
-
-
Save Classy-Bear/afef129e9b878eb3202f47434c822ed4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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