Easiest way to save files in Flutter.
Hey guys, today we’ll see the easiest way to save files on any OS in Flutter (including Web).
We’ll use the file_saver Plugin that I’ve developed only for this sole purpose so let’s dive into it.
The plugin right now in a development stage but can do what we need, So it’s really simple to use the plugin.
What you need?
Encoded file in Uint8List (We will see this later), know your file extension, and your file name.
And MimeType which is optional and important for web, you can give mimeType with MimeType enum given in the plugin, there are many mimetypes for common files. Will keep adding more.
PS: Now there’s a custom option for using whatever mimeType you want (it should be supported by OS)
How to use it?
Just simply call the method :
FileSaver.instance.saveFile({
required String name,
Uint8List? bytes,
File? file,
String? filePath,
LinkDetails? link,
String ext = "",
MimeType mimeType = MimeType.other,
String? customMimeType});
What about Storage Permissions?
There are some platform specific modifications that you need to do for permissions that you’ll find on pub.dev.
Now, let’s see how we’ll do it:
First we’ll create a file:
File file = File('myText.txt');
Then we’ll write in our ‘myFile.txt’
await file.writeAsString("This is Our File");
Now, We’ll encode it into Uint8List (Optional)
Uint8List bytes = await file.readAsBytes();
And here we are almost done here
We can call this method in multiple ways:
Case 1: You have an object of bytes
FileSaver.instance.saveFile(
name: 'mytext',
bytes: bytes,
ext: 'txt',
mimeType: MimeType.text);
Case 2: You have an object of file
FileSaver.instance.saveFile(
name: 'mytext',
file: file,
ext: 'txt',
mimeType: MimeType.text);
Case 3: You have a link
FileSaver.instance.saveFile(name: 'mytext', link: LinkDetails(
link: 'https://location.to.your/file',
headers: {
"Headers" : "If you need any",
}
),
ext: 'txt',
mimeType: MimeType.text);
If you need custom mimeType, you have to choose custom from the mimeType enum and enter your mimeType
FileSaver.instance.saveFile(
name: 'mytext',
file: file,
ext: 'txt',
mimeType: MimeType.custom,
customMimeType: "application/custom-type"
);
And boom the file is saved in Downloads folder in all OS and will download the file itself in web.
Sorry for not using gist for code
̶T̶h̶i̶s̶ ̶w̶i̶l̶l̶ ̶p̶l̶u̶g̶i̶n̶ ̶i̶s̶ ̶u̶n̶d̶e̶r̶ ̶d̶e̶v̶e̶l̶o̶p̶m̶e̶n̶t̶ ̶f̶o̶r̶ ̶a̶ ̶S̶a̶v̶e̶ ̶A̶s̶ ̶d̶i̶a̶l̶o̶g̶ ̶f̶o̶r̶ ̶a̶l̶l̶ ̶p̶l̶a̶t̶f̶o̶r̶m̶.̶
Well, we have the saveAs method now, currently working for Android and iOS, it opens a file manager to let you save the file.
The method:
await FileSaver.instance.saveAs({
required String name,
Uint8List? bytes,
File? file,
String? filePath,
LinkDetails? link,
required String ext,
required MimeType mimeType,
String? customMimeType
});
This method is exactly same as the saveFile method explain above, the usage is also same, but it will open a file manager to save the file to specific location and it currently works on Android and iOS.