Sign in
Topics
Build Flutter app with local data storage
Learn how to set up Flutter Hive for fast, offline local data storage. This guide covers initialization, Hive boxes, CRUD operations, type adapters, and security options to help you manage both simple and complex data in your Flutter applications.
What if your Flutter app could store and retrieve data instantly, even offline?
Hive makes that possible. This fast NoSQL database, built entirely in Dart, lets you handle everything from simple key-value pairs to complex objects with minimal setup. In this guide, you’ll learn how to set up Hive, work with boxes, perform CRUD operations, and apply best practices for smooth local data storage in Flutter.
Hive is a fast NoSQL database. It works as a pure Dart, local data storage solution that handles key-value pairs. You initialize Hive with a simple setup and then create a Hive box to store objects or primitive types. Hive storage uses a separate file per box. You can store data offline with multi-isolate support and enjoy a key-value store that loads quickly.
When you write a Flutter app, Hive gives you offline support. It’s a local data storage system built for Dart object handling with no native dependencies. You open a box, then use CRUD operations to store and retrieve data easily. A final box remains open until you explicitly close boxes, making lifecycle control clear.
Hive stands out because it supports multi-isolation and lets you handle complex types with type adapter generation. It runs entirely in Dart. You avoid bridging to native modules or SQLite. That pure Dart approach means easier storage of data and retrieval of data workflows, and secure NoSQL database Hive usage. In many cases, Hive offers better raw performance than SQLite, especially for simple key-value use cases.
Is Flutter Hive NoSQL? Yes—that is the nature of Hive. It stores data in a key-value store rather than tables. For certain apps, SQLite may still fit, but when you want local data storage, fast NoSQL database use, and type safety, Hive works very well. Is Hive good for Flutter? Yes, it utilizes Flutter-friendly constructs such as async, type adapters, and final boxes, and integrates smoothly into Flutter app lifecycles.
Get started with Hive by following these steps.
After describing basic concepts, here’s practical code:
Set up in your void main:
1void main() async { 2 WidgetsFlutterBinding.ensureInitialized(); 3 await initializeHive(); 4 final box = await Hive.openBox('myBox'); 5 runApp(const MyApp()); 6} 7
You import Hive and call initialize Hive (your method to set up the path and register adapters). That final box becomes your gateway to store and retrieve data. After that, you use 'box.put' and 'box.get' for key-value operations.
Before writing code, add dependencies in pubspec.yaml:
1dependencies: 2 flutter: 3 sdk: flutter 4 hive: ^2.0.0 5 hive_flutter: ^1.1.0 6dev_dependencies: 7 hive_generator: ^1.1.0 8 build_runner: ^2.1.0 9
Then run the following command to generate a type adapter for your Dart object:
1flutter pub run build_runner build 2
That creates type adapter code to let Hive store complex types beyond basic strings or ints.
Ready to turn your Hive-powered Flutter app into a production-ready product faster?
Build, test, and deploy instantly with Rocket.new—your all-in-one Flutter development accelerator.
Follow these steps to store objects:
1(typeId: 0) 2class Person { 3 (0) 4 String name; 5 6 (1) 7 int age; 8 9 Person({required this.name, required this.age}); 10} 11
Run the command above to generate the adapter. Then register it before opening a hive box:
1Hive.registerAdapter(PersonAdapter()); 2
Now you can store a Dart object:
1final box = await Hive.openBox('people'); 2final person = Person(name: 'Alice', age: 30); 3box.put('user1', person); 4
You can retrieve data:
1var stored = box.get('user1') as Person; 2print(stored.name); // string name 3print(stored.age); // int age 4
A final box stays open until you close all boxes. At shutdown, call:
1await Hive.close(); 2
That keeps storage clean.
After initializing Hive and opening a box:
Create / Update:
1box.put('key', 'value'); 2
Read:
1var val = box.get('key'); 2
Delete:
1box.delete('key'); 2
These are key value operations. Hive also supports complex types via a type adapter.
You can have multiple hive box objects open, even storing a separate file for each. That helps organize user data, settings, or sensitive data in distinct boxes. You can easily store and retrieve data per context.
Hive supports a secure NoSQL database through optional encryption. You can add an encryption key when creating a box:
1var box = await Hive.openBox( 2 'secureBox', 3 encryptionCipher: HiveAesCipher(mySecureKey), 4); 5
That lets you store sensitive data safely in a key-value store. The performance remains high with minimal latency. For a Flutter app needing offline support and fast data storage, Hive fits well. SQLite may remain better when you need complex relational queries—but when speed and simplicity matter, Hive shines.
Use a type adapter to store complex types. Hive supports multi-isolate support. You can access boxes from background isolates.
Hive is under the Apache license. It suits many use cases under applicable law and corporate policies.
Hive has become a go-to NoSQL database for Flutter apps needing quick, local data storage. It’s pure Dart, works offline, stores key-value pairs or complex Dart objects in boxes, and even supports encryption.
Use Hive when: you need fast reads/writes, offline storage, simple key-value or object data, or lightweight persistence without native dependencies.
Avoid Hive when: you need complex relational queries, huge datasets, or frequent schema changes—use SQLite or Firestore instead.
“Tables in the hive metastore do not benefit from the full set of security and governance features provided by the unity catalog. such as built in auditing, lineage and access control.”— LinkedIn Post
This guide showed how to initialize Hive, manage Hive box, store, and retrieve data in a Flutter project. You can store key-value pairs, complex types with type adapters, and even sensitive data with encryption.
Hive provides offline support, multi-isolate support, and swift local data storage for Flutter applications. Pick Hive for small to medium offline-first apps; switch to a more structured database as complexity grows. By applying these practices in your Flutter Hive setup, you’ll create maintainable, fast, and reliable data workflows.