4 Ways to Store Data Offline in Flutter

Last updated on February 24, 2021 A Goodman Loading... Post a comment

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.

To get the path correctly, we can use a package name 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.

SQLite

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 UPDATE query:

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

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.

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');

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

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x