Open In App

Flutter – Read, Write and Override File Locally to Disk

Last Updated : 09 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In many cases, we need to save files locally so that we can read and write them while the device is offline. For example, we might need to persist data across the app. To save files to disk on mobile or desktop apps, we need to add package path_provider to our project. There are Four Steps to Read and Write files to a local disk.

  • Step 1: Find the local Path
  • Step 2: Create an Address to the File’s Location
  • Step 3: Write data to File.
  • Step 4: Read data from the File.

A sample video is given below to get an idea about what we are going to do in this article. The video below shows that data entered by the user persists even after closing the app from the background.

Let’s Build UI to Show Saved Credentials

Creating HomePage class by extending stateful widget to build below UI.

1

Dart




import 'package:flutter/material.dart';
import 'package:readwritegfg/credpage.dart';
import 'package:readwritegfg/readwrite.dart';
  
class HomePage extends StatefulWidget {
  const HomePage({super.key});
  
  @override
  State<HomePage> createState() => _HomePageState();
}
  
String username = "Username saved by user will be visible here";
String userPass = "Password saved by user will be visible here";
String readName = "";
String readPass = "";
  
Future<void> getCred() async {
  ReadWrite read = ReadWrite();
  readName = await read.readTextFile('username.txt');
  readPass = await read.readTextFile('userPass.txt');
}
  
class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.lightBlue,
        title: const Text(
          "Saved Credentials",
          style: TextStyle(fontSize: 40),
        ),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: Column(
          children: [
            Text(
              "Username",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            Text(
              username,
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            Text(
              "Password",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            Text(
              userPass,
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            ElevatedButton(
              onPressed: () async {
                await getCred();
                setState(() {
                  if (readName.isNotEmpty && readPass.isNotEmpty) {
                    username = readName;
                    userPass = readPass;
                  }
                });
              },
              style: ElevatedButton.styleFrom(backgroundColor: Colors.blue),
              child: const Text(
                "Click here to See Credentials",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            ),
            SizedBox(
              height: 50,
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => const CredPage()));
              },
              style: ElevatedButton.styleFrom(backgroundColor: Colors.blue),
              child: const Text(
                "Click here to Change Credentials",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            )
          ],
        ),
      ),
    ));
  }
}


Let’s Build UI to Save User Input To Local Disk

Creating CredPage class by extending stateless widget to build below UI.

2

Dart




import 'package:flutter/material.dart';
import 'package:readwritegfg/readwrite.dart';
  
class CredPage extends StatelessWidget {
  const CredPage({super.key});
  @override
  Widget build(BuildContext context) {
    TextEditingController getUserName = TextEditingController();
    TextEditingController getUserPassword = TextEditingController();
    return SafeArea(
        child: Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.lightBlue,
        title: const Text(
          "Credentials",
          style: TextStyle(fontSize: 40),
        ),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: Column(
          children: [
            Text(
              "Username",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            TextField(
              controller: getUserName,
              decoration: const InputDecoration(hintText: "Username"),
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            Text(
              "Password",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            TextField(
              controller: getUserPassword,
              decoration: const InputDecoration(hintText: "Password"),
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            ElevatedButton(
              onPressed: ()async {
                final store = ReadWrite();
              await  store.writeTextFile('username.txt', getUserName.text);
              await  store.writeTextFile('userPass.txt', getUserPassword.text);
              },
              style: ElevatedButton.styleFrom(backgroundColor: Colors.blue),
              child: const Text(
                "Click here to Save Credentials",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            )
          ],
        ),
      ),
    ));
  }
}


Lets Code to Store Inputted Data by User

Create Class to write code to Store and Read Text from local disk:

As, discussed above in this article, to save files to disk on mobile or desktop apps, we need to add package path_provider to our project.

Step 1: Inside pubspec.yaml add path_provider: ^2.1.1

dependencies:
flutter:
sdk: flutter
path_provider: ^2.1.1

Step 2: Run flutter pub get in terminal

flutter pub get

Step 3: Find Local Path to store your file

Using path_provider we can find path on local storage to Write and Read our application files. We can use getApplicationDocumentsDirectory() to get path of the local storage where our application can store files locally, it is not necessarily visible to user through device file manager. Else, getExternalStorageDirectory() can be used, It returns directory on External Local Storage (SD card on Android) which might be available or not in user’s device.

 Future<String> get _directoryPath async {
Directory? directory = await getApplicationDocumentsDirectory();
return directory.path;
}


Step 4: Create Address to File’s Location

Here we will Create File , (for example: username.txt) so that we can write and read in that file in future.

  Future<File> getFile(String fileNameWithExtension) async {
final path = await _directoryPath;
return File("$path/$fileNameWithExtension");
}

Step 5: Write Data to Text file

Now, we have file where we can write, override and read.

writeTextFile(String fileNameWithExtension, String content) async {
File file = await getFile(fileNameWithExtension);
file.writeAsString(content);
}

Step 6: Read Data From Text File

Now, you have data on your disk, you can read it.

 readTextFile(String fileNameWithExtension) async {
final file = await getFile(fileNameWithExtension);
final fileContent = await file.readAsString();
return fileContent;
}

Step 7: Create class ReadWrite and code all the above functions in one class

(class can have any name as per your preference)

Dart




import 'dart:io';
  
import 'package:path_provider/path_provider.dart';
  
class ReadWrite {
  Future<String> get _directoryPath async {
    Directory? directory = await getApplicationDocumentsDirectory();
    return directory.path;
  }
  
  Future<File> getFile(String fileNameWithExtension) async {
    final path = await _directoryPath;
    return File("$path/$fileNameWithExtension");
  }
  
  writeTextFile(String fileNameWithExtension, String content) async {
    File file = await getFile(fileNameWithExtension);
    file.writeAsString(content);
  }
  
  readTextFile(String fileNameWithExtension) async {
    final file = await getFile(fileNameWithExtension);
    final fileContent = await file.readAsString();
    return fileContent;
  }
}


Let’s Merge UI and Backend Code

Step 1: Storing Data from CredPage

Create object of the ReadWrite class in CredPage class, and implement the function writeTextFile. store is the object of ReadWrite class through which we can access functions inside ReadWrite class. Text from TextEditingController will get stored in file with name username.txt (or whatever you provide here)

read_write

Dart




import 'package:flutter/material.dart';
import 'package:readwritegfg/readwrite.dart';
  
class CredPage extends StatelessWidget {
  const CredPage({super.key});
  @override
  Widget build(BuildContext context) {
    TextEditingController getUserName = TextEditingController();
    TextEditingController getUserPassword = TextEditingController();
    return SafeArea(
        child: Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.lightBlue,
        title: const Text(
          "Credentials",
          style: TextStyle(fontSize: 40),
        ),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: Column(
          children: [
            Text(
              "Username",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            TextField(
              controller: getUserName,
              decoration: const InputDecoration(hintText: "Username"),
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            Text(
              "Password",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            TextField(
              controller: getUserPassword,
              decoration: const InputDecoration(hintText: "Password"),
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            ElevatedButton(
              onPressed: ()async {
                /* store is the object of ReadWrite class 
                through which we can access functions inside ReadWrite 
                hence, store.writeTextFile('username.txt',getUsername.text) 
                Text from TextEditingController will get stored in file with name username.txt (or whatever you provide here) */
                final store = ReadWrite();
              await  store.writeTextFile('username.txt', getUserName.text);
              await  store.writeTextFile('userPass.txt', getUserPassword.text);
              },
              style: ElevatedButton.styleFrom(backgroundColor: Colors.blue),
              child: const Text(
                "Click here to Save Credentials",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            )
          ],
        ),
      ),
    ));
  }
}


Dart




ElevatedButton(
              onPressed: ()async {
                /* store is the object of ReadWrite class 
                through which we can access functions inside ReadWrite 
                hence, store.writeTextFile('username.txt',getUsername.text) 
                Text from TextEditingController will get stored in file with name username.txt (or whatever you provide here) */
                final store = ReadWrite();
              await  store.writeTextFile('username.txt', getUserName.text);
              await  store.writeTextFile('userPass.txt', getUserPassword.text);
              },


Step 2: To read the saved Credentials in HomePage, create object of ReadWrite class and implement readTextFile.

Future<void> getCred() async {
/* read is object of ReadWrite class through which we will access stored data in file*/
ReadWrite read = ReadWrite();
readName = await read.readTextFile('username.txt');
readPass = await read.readTextFile('userPass.txt');
}

Dart




import 'package:flutter/material.dart';
import 'package:readwritegfg/credpage.dart';
import 'package:readwritegfg/readwrite.dart';
  
class HomePage extends StatefulWidget {
  const HomePage({super.key});
  
  @override
  State<HomePage> createState() => _HomePageState();
}
  
String username = "Username saved by user will be visible here";
String userPass = "Password saved by user will be visible here";
String readName = "";
String readPass = "";
  
Future<void> getCred() async {
  ReadWrite read = ReadWrite();
  readName = await read.readTextFile('username.txt');
  readPass = await read.readTextFile('userPass.txt');
}
  
class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.lightBlue,
        title: const Text(
          "Saved Credentials",
          style: TextStyle(fontSize: 40),
        ),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: Column(
          children: [
            Text(
              "Username",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            Text(
              username,
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            Text(
              "Password",
              style: TextStyle(fontSize: 30, color: Colors.green.shade900),
            ),
            Text(
              userPass,
              style: TextStyle(fontSize: 30, color: Colors.orange.shade900),
            ),
            const SizedBox(
              height: 50,
            ),
            ElevatedButton(
              onPressed: () async {
                await getCred();
                setState(() {
                  if (readName.isNotEmpty && readPass.isNotEmpty) {
                    username = readName;
                    userPass = readPass;
                  }
                });
              },
              style: ElevatedButton.styleFrom(backgroundColor: Colors.blue),
              child: const Text(
                "Click here to See Credentials",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            ),
            SizedBox(
              height: 50,
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => const CredPage()));
              },
              style: ElevatedButton.styleFrom(backgroundColor: Colors.blue),
              child: const Text(
                "Click here to Change Credentials",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            )
          ],
        ),
      ),
    ));
  }
}


Dart




Future<void> getCred() async {
  /* read  is object of ReadWrite class through 
       which we will access stored data in file*/
  ReadWrite read = ReadWrite();
  readName = await read.readTextFile('username.txt');
  readPass = await read.readTextFile('userPass.txt');
}


To Rewrite/Override just save the data again to that referenced file location, In the below output it is shown that saved data is overridden and data stored by user is persisted.

Output:



Similar Reads

Delete a locally Uploaded File on Google Colab
In this article, we will learn to delete locally uploaded folders in our Google Colab notebook. You can also refer to the video solution for this end which is attached at the end of this article. Delete Local Uploaded file on Google ColabPrerequisite for the task - A Google Account. Refer to this article which explains how to create a Gmail and acc
2 min read
Flutter - Read and Write Data on Firebase
Firebase helps developers to manage their mobile app easily. It is a service provided by Google. Firebase has various functionalities available to help developers manage and grow their mobile apps. In this article, we will learn how to write and read data into/from firebase. We will be using flutter for this. To do this job we need to follow the 3-
4 min read
Read and Write Data in Flutter using SharedPreferences
SharedPreferences is the best option to store a small amount of data in flutter projects. Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, integer, float, Boolean that make up your preferences in an XML file inside the app on the devi
4 min read
Locally Linear Embedding in machine learning
LLE(Locally Linear Embedding) is an unsupervised approach designed to transform data from its original high-dimensional space into a lower-dimensional representation, all while striving to retain the essential geometric characteristics of the underlying non-linear feature structure. LLE operates in several key steps: Firstly, it constructs a neares
8 min read
Tailwind CSS 3 Classes doesn't Override Previous Classes
Tailwind CSS is basically a Utility-first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. Syntax&lt;div class="bg-blu
2 min read
How to override React Bootstrap active Tab default border styling?
React-Bootstrap provides various components with basic default styling. Sometimes, there is a need to override those basic default styles as per requirements. In this article, let us discuss overriding that default styling for the border of the active tab in the navbar. PrerequisitesReact JSReact-BootstrapSteps to create React Application and insta
3 min read
Read And Write Tabular Data using Pandas
Pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental, high-level building block for doing practical, real-world data analysis in Python.The two primary data structures of Pandas, Series (1-dimensional) a
3 min read
Flutter - Read JSON Data from Assets Folder
A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by a colon. Each entry (key/value pair) is separated by a comma. We will learn how to read the data already stored in your project folder in your app with just a simple function. Sometimes it happens that
3 min read
Is Flutter Worth Learning? Top 7 Reasons to Learn Flutter
In today's era, the usage of smartphones has increased exponentially and so has the use of mobile applications and websites. Meanwhile, considering future career prospects, learning web development and mobile app development is strongly recommended for all individuals. And when we come to mobile app development, there are two most-popular open-sour
5 min read
Flutter - Sharing Data Among Flutter Pages
In this article, we are going to find the solution for the problem statement "Import/send data from one page to another in flutter". Before going into the topic, there are some pre-requisites. Pre-requisites:Basics of dart programming language.Setting up Flutter in VS code or Setting up Flutter in Android Studio.Creating first flutter app | Hello W
4 min read