How to Encode/Decode Path, File, Bytes and Base64 in Dart/Flutter

In this example, we are going to show you the basic handling of files in Dart and Flutter. We are going to decode or encode Path to file, file to Bytes, Bytes to Base64 String, or vice versa. 

import 'dart:io';
String imgpath = "data/0/image.jpg";
File imgfile = File(imagepath);

import 'dart:io';
String path = imgfile.path;

import 'dart:io';
import 'dart:typed_data';
Uint8List imgbytes = await imagefile.readAsBytes();
//OR
Uint8List imgbytes1 = imagefile.readAsBytesSync();

import 'dart:io';
import 'dart:typed_data';
File decodedimgfile = await File("image.jpg").writeAsBytes(imgbytes);

import 'dart:convert';
import 'dart:typed_data';
String bs4str = base64.encode(imgbytes);
//OR
String bs4str1 = base64Encode(imgbytes);

import 'dart:convert';
import 'dart:typed_data'; //for bytes Uint8List
Uint8List decodedbytes = base64.decode(bs4str);
//OR
Uint8List decodedbytes1 = base64Decode(bs4str);

String imgpath = "data/0/image.jpg";
File imgfile = File(imagepath);
Uint8List imgbytes = await imagefile.readAsBytes();
String bs4str = base64.encode(imgbytes);

String bs4str = "/9j/4QejRXhpZgAASUkqAAgAAAAUACACBAABAA....";
Uint8List decodedbytes = base64.decode(bs4str);
File decodedimgfile = await File("image.jpg").writeAsBytes(decodedbytes);
String decodedpath = decodedimgfile.path;

In this way, you can handle files in Dart/Flutter.

No any Comments on this Article


Please Wait...