5 Ways to Store Data in Flutter App Offline/Locally
5 Ways to Store Data in Flutter App Offline/Locally

Hi Guys, Welcome to Proto Coders Point. In this article on Flutter Dart let us know all the ways by which we can store data offline locally in Flutter App itself. Let’s get Started.

There are n number of scenarios where the best practice is storing data locally on users devices rather than relying on remote cloud servers or APIs, if we store frequently used data from API call then it can also lead to increase server cost, so it’s better to store as a cache into local database in flutter app . For Example, your flutter app might want to store data between app sessions or download a language dictionary from the internet for offline access.

Flutter SQLite

SQLite is a lightweight, fast, self-contained, and reliable SQL database engine written in C. It is embedded in all mobile devices and is commonly included in many applications used daily.

For beginners, SQLite queries might seem complex, but it simplifies operations like INSERT, READ, UPDATE, and DELETE basicallyt using SQLite we can perform CRUD operation locally in mobile devices.

Key Note:

Flutter SQLite library is very small, I means the The library size (engine that runs SQLite) is 400–500 KB and the data storage capacity depends on the memory size the mobile device is allocated.

Example of Flutter SQLite

In Futter, We have a package/Plugin using which you can work with SQLite DB i.e. sqflite


Hive Database in flutter

Hive Database is built using pure DART language, Hive DB is a lightweight & fast key-value NOSQL Database. Which is similar to MongoDB, HIVE DB can be used as best alternative of SQLite Database.

To make use of HIVE Database in Flutter we must download 2 package Hive & hive_flutter

More Implementation details you can check the article: Link

Hive make use of box in which we can store the data we need by using put or get method

Example:

var box = Hive.box('myBox'); // Create a box
box.put('name', 'Rajat'); // store the data in key-value pair
var name = box.get('name');  //retrive the data using get by passing key
print('Name: $name'); // Rajat

Shared Preferences Storage in flutter

Shared Preferences is the most widely used plugin as a simple storage medium. It make use of NSUserDefaults on iOS and macOS and SharedPreferences on Android. While data can be saved to disk asynchronously, it is not guaranteed that writes will persist immediately.

Shared Preferences is best to use for non-critical data storage, may be to keep user logged into the app, user settings or small key-value pairs.

Syntax Example:

SharedPreferences prefs = await SharedPreferences.getInstance();

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

Read our Tutorial about how to use Shared Preferences in Flutter App

Flutter Sharedpreferences Alternative | GetX Storage | keep user loggedIn
Shared preferences to keep user logged in flutter app


ObjectBox in flutter

ObjectBox is basically a dart-native key-value database, which is some what similar to Hive. It is designed for speed query , enhancing the data response times and ObjectBox also supporting real-time applications. In Flutter Dart ObjectBox Highly compatible with OS like: Android, iOS, macOS, Linux, and Windows, offering built-in support for object links and relationships.

Although it delivers excellent performance, But as ObjectBox is still new in database market it required time to mature and prove its reliability in large-scale projects.

A Quick Example on ObjectBox in Flutter Dart:

@Entity()
class PersonData {
  int id;

  String firstName;
  String lastName;

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

final store = await openStore(); 
final box = store.box<PersonData>();  // Creation on ObjectBox Box

var personObj = PersonData(firstName: 'John', lastName: 'Doe');

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

personObj = box.get(id)!;       // Read or simple get the data stored in Object

personObj .lastName = "Doe Doe Doe";
box.put(personObj );             // Update the lastname

box.remove(personObj .id);  

Realm Database in flutter

Realm is an open-source free to use Database, mobile-first database designed to store offline data persistence in Flutter applications. Developed by the same company behind MongoDB, Realm in Flutter offers a NoSQL key-value structure, making it a powerful tool for mobile app development.

To add Realm to your Flutter project, simply run:

flutter pub add realm

Here is an Example on Realm is object-oriented data model, The Data Structure is Data Model is Defined as below:

import 'package:realm/realm.dart';  
part 'app.g.dart'; 

// define a data model class named _User
@RealmModel() 
  class _User {
    late String emal;

    late String name;

    int? age = 18;
}

Then you need to generate a RealMObject Class, To do that just run execute below command:

flutter pub run realm generate

Then now you can create and save the new user into your RealM Database as below:

var config = Configuration.local([User.schema]);
var realm = Realm(config);

var userOne = User("hello@kindacode.com", "Mr. Hello", age: 100);
  realm.write(() {
    realm.add(userOne);
});

Consultion

In this article we check different way by which we can store data locally/Offline into Mobile device using Mobile Storage.