Write To and Read From File In Flutter
Lakshydeep Vikram S.
Software Dev | Constultant | Author @ Google Dev Library | Author: Make Yourself The Software Developer
In Flutter you will get scenario for how to write and read from files. This is how you will achieve.
Write to File
final File file = File(<path>);
file.writeAsString(<data>);
if (await Permission.manageExternalStorage.isGranted){
final path = Directory('/storage/emulated/0/30FlutterTips/');
String res = "";
if (await path.exists()){
res = path.path;
} else {
final Directory appDocDirNewFolder = await path
.create(recursive: true);
res = appDocDirNewFolder.path;
}
final File file = File("${res}tips22.dart");
await file.writeAsString(controller.text.toString());
} else {
await Permission.manageExternalStorage.request();
}
Read From File
For reading text from file, we can use the following methods:
File(<path>).readAsString();
if (await Permission.manageExternalStorage.isGranted){
await File("/storage/emulated/0/30FlutterTips/tips22.dart")
.readAsString();
} else {
await Permission.manageExternalStorage.request();
}
The whole code had been uploaded here:
You can check my videos here:
You can follow me on medium:
You can read book as well:
Stay Tuned.
Any feedback will be appreciated.
Share and follow the tag:?#30FlutterTips
Let me know some topics you want me to write.
Follow me on:?LinkedIN?|?GitHub?|?Google DevLibrary?|?YouTube
Stay in touch for more Flutter tips.
Thanks.