How to save Base64 String to file and view it using Flutter

13,500

Following is the code snippet to decode base64 string and save it as a file on the local device. please note in terms of viewing files, image and pdf would require different libraries.

Future<String> _createFileFromString() async {
final encodedStr = "put base64 encoded string here";
Uint8List bytes = base64.decode(encodedStr);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = File(
    "$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + ".pdf");
await file.writeAsBytes(bytes);
return file.path;
 }
Share:
13,500
esthrim
Author by

esthrim

Updated on July 18, 2022

Comments

  • esthrim
    esthrim almost 2 years

    I need to download and view file (if possible, for Image, PDF, etc) using Flutter. My problem is, the file that I want to download is Base64 String. How can I achieve that using Flutter??

  • Boni Machado
    Boni Machado almost 3 years
    Here, you have a base64 string and create the file. But would you happen to know how to do the inverse, pal? I've an image file path and need to convert it to base64 in a flutter web app for submitting.
  • biniyam112
    biniyam112 over 2 years
    did you got the solution to this??
  • Ahmad Khan
    Ahmad Khan almost 2 years
    @BoniMachado check this out stackoverflow.com/a/50038239/10482516