Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Flutter Read/Write File Example – with path_provider and dart:io
https://grokonez.com/flutter/flutter-read-write-file-example-path-provider-dartio-example
Flutter Read/Write File Example – with path_provider and dart:io
In this tutorial, we're gonna build a Flutter App that can read file and write data to file for later use. To do this, we need to combine path_provider plugin with dart:io library.
Flutter App Overview
Our Flutter App has a Text field, when we click on Write to File button, the String in Text field will be written to text.txt file (appending text) and display on Screen.
Everytime we launch the App, it read text.txt file and show contents inside.
We can also clear content of the file by Clear Contents button.
Read/Write Files
Find Local Path
We need a place to write data on disk and read it again when the app loads.
So we use path_provider plugin to access Documents directory (on iOS, this corresponds to
NSDocumentDirectory
, on Android, this is theAppData
directory).Future get _localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; }
Reference to File
To create a reference to the File full location (in our case, the text.txt file), we use File class from the dart:io library.
Future get _localFile async { final path = await _localPath; return File('$path/text.txt'); }
Write data to File
We will write a string to a file using File
writeAsString()
method. It returns aFuture<File>
that completes with thisFile
object once the entire operation has completed.
More at:
Flutter Read/Write File Example – with path_provider and dart:io
https://grokonez.com/flutter/flutter-read-write-file-example-path-provider-dartio-example







