Flutter File Read And Write Operations
File recording operations have become an indispensable feature of today’s applications. We use this feature in many areas in our applications, such as saving the user’s settings, saving sign in / sign up properties, or saving / reading the user’s files.
There is no special library to use for file operations, but we will use the path_provider library to learn the paths of the files we will process.
Let’s start by adding the path_provider library to our pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
path_provider:
If we are going to do our file operations outside of the application’s files, we need to get permission to access the files from the phone. The library we need to use for this is the permission_handler library.
permission_handler:
After adding our libraries, let’s add the necessary permissions to read and write our files on the android side.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Performing the necessary permissions and library operations, we can now move on to the code part.
Write to File
To write to the file, we get the location of our application’s files by using the getApplicationDocumentsDirectory function from the path_provider library. I want to save it in the example.txt file, I specified my file path and variable this way. Afterwards, we determine the text to be saved and print this text to the file with the writeAsString function.
onPressed: ()async{
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/example.txt');
String write_text = "sample text";
file.writeAsString(write_text);
},
If you are using Android Studio as your IDE, you can see that it saves our file to data > data > app_package_name > app_flutter via Device File Explorer.
Read from File
For reading operations from the file, as well as for writing operations, we get the location of the files of our application’s files by using the getApplicationDocumentsDirectory function of our path_provider library. Then, we define our variable by writing the name of the file we will read after this file path. I will use the file I used in the write operation for the read operations.
onPressed: ()async{
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/example.txt');
String read_text = await file.readAsString();
print(read_text);
},
Sample Application
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
TextEditingController controller_write = TextEditingController();
TextEditingController controller_read = TextEditingController();
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text("Local Storage Example"),
),
body: Container(
child: Column(
children: [
Container(
padding: EdgeInsets.all(20),
child: TextField(
controller: controller_write,
decoration: InputDecoration(
border: OutlineInputBorder()
),
)),
Container(child: ElevatedButton(
child: Text("Write"),
onPressed: ()async{
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/example.txt');
String write_text = controller_write.text;
file.writeAsString(write_text);
},
)),
Container(child: ElevatedButton(
child: Text("Read"),
onPressed: ()async{
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/example.txt');
String read_text = await file.readAsString();
controller_read.text = read_text;
},
)),
Container(
height: MediaQuery.of(context).size.height*0.5,
padding: EdgeInsets.all(20),
child: TextField(
controller: controller_read,
readOnly: true,
maxLines: 40,
decoration: InputDecoration(
border: OutlineInputBorder()
),
)),
],
),
),
);
}
}