16.1 C
New York
Saturday, April 1, 2023

5 Ways to Store Data Offline in Flutter

There are many cases where you need to store data locally on users’ devices instead of using remote cloud servers or APIs, for example, you may need to persist data across app launches, or download a language dictionary from the internet and save it for later offline use. This article will walk you through 4 solutions to do that.

Using Text/CSV/JSON files

The most common file types used for saving data are TXT, CSV, and JSON. You can use one or some of them for your applications. The location to place the data files is the Documents directory of a device. On Android, it is the AppData directory and on iOS, it is NSDocumentDirectory.Advertisements

To get the path correctly, we can use a package named path_provider.

Sample code:

Future<String> get _getLocalPath async {
  final dir = await getApplicationDocumentsDirectory();
  return dir.path;

Exiting the application, restarting, or shutting down the device does not lose data. Data is only deleted when the corresponding application is deleted.

AdvertisementsSee also: Flutter: How to Read and Write Text Files


SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. It is built into all mobile phones and comes bundled inside countless other applications that people use every day.

SQLite queries can be confusing for people who are new to them, but using SQLite will help you easily perform the operations of INSERT, READ, UPDATE, and DELETE with large amounts of data.


In Flutter, you can interact with SQLite by using the sqflite plugin (the name contains an “f” character). Below is an example of an UPDATE query:

int count = await database.rawUpdate(
    'UPDATE Test SET name = ?, value = ? WHERE name = ?',
    ['updated name', '9876', 'some name']);

Besides the widely-used sqflite, there’re a couple of nice packages for working with SQLite, such as drift and floor.

For more details on using SQLite in Flutter, see this article.

Hive Database

If you have worked with MongoDB before then you will probably enjoy using the HIVE database. This is a fast, lightweight, NoSQL, key-value database written in pure Dart. You can work it just like a map:

var box = Hive.box('myBox');
box.put('name', 'David');
var name = box.get('name');
print('Name: $name');

Generally, using Hive database is more simple than using SQLite. You can learn how to perform CRUD operations with Hive and Flutter in this article: Flutter & Hive Database: CRUD Example.

Shared Preferences Storage

Shared preferences is a popular plugin that provides platform-specific persistent storage for simple data (NSUserDefaults on iOS and macOS, SharedPreferences on Android). Data may be persisted to disk asynchronously, and there is no guarantee that writes will be persisted to disk after returning, so this plugin must not be used for storing critical data.

Sample usage:

SharedPreferences prefs = await SharedPreferences.getInstance();

// Reading data
String name = prefs.getString('name');
// Updating
await prefs.setString('name', 'Some Value');


Similar to Hive, Objectbox is a Dart-native key-value database. It’s extremely fast and improves response rates as well as enables real-time applications. The database supports Android, iOS, macOS, Linux, and Windows and delivers built-in object links/relationships.

Objectbox is quite young and it needs time to mature and be battle-tested in large projects.

A quick example:

class Person {
  int id;

  String firstName;
  String lastName;

  Person({this.id = 0, required this.firstName, required this.lastName});

final store = await openStore(); 
final box = store.box<Person>();

var person = Person(firstName: 'John', lastName: 'Doe');

final id = box.put(person);  // Create

person = box.get(id)!;       // Read

person.lastName = "Doe Doe Doe";
box.put(person);             // Update


Wrapping Up


This article introduces the most popular offline data storage methods today in Flutter applications. You can see more detailed instructions and examples in the following articles:

You can also check out our Flutter topic page or Dart topic page for the latest tutorials and examples.


Related Articles

Latest Articles