What kind of database is Hive?
Hive is a NoSQL key-value database written in pure Dart with very fast read/write speeds.
Use Hive for fast, lightweight NoSQL storage in Flutter
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Hive is a lightweight, high-performance key-value database written entirely in pure Dart. Unlike SharedPreferences, Hive can store complex custom objects, not just primitive types. It works on all platforms (mobile, web, desktop) and reads data from memory, making it significantly faster than SQLite for most use cases. Hive is ideal for caching API responses, storing user profiles, saving app state, or any structured data that does not require relational queries like JOINs.
1// pubspec.yaml: hive: ^2.2.3, hive_flutter: ^1.1.02// dev_dependencies: hive_generator: ^2.0.0, build_runner: ^2.4.03import 'package:hive_flutter/hive_flutter.dart';45// ── Initialize Hive ──6void main() async {7await Hive.initFlutter();8Hive.registerAdapter(UserAdapter()); // for custom objects9runApp(const MyApp());10}1112// ── Basic Box (key-value) ──13final box = await Hive.openBox('settings');14box.put('theme', 'dark');15box.put('language', 'en');1617final theme = box.get('theme', defaultValue: 'light');18box.delete('theme');1920// ── TypeAdapter for custom objects ──21@HiveType(typeId: 0)22class User extends HiveObject {23@HiveField(0)24late String name;2526@HiveField(1)27late int age;2829@HiveField(2)30late String email;31}3233// ── Store custom objects ──34final userBox = await Hive.openBox<User>('users');35final user = User()36..name = 'Ahmed'37..age = 2538..email = 'ahmed@email.com';3940await userBox.add(user); // auto-generated key41await userBox.put('user1', user); // custom key42final allUsers = userBox.values.toList();
After creating a HiveType class, run flutter pub run build_runner build in your terminal. This generates the TypeAdapter code automatically so Hive knows how to serialize and deserialize your custom objects.
Hive is fast, but structure matters as projects grow. Make explicit decisions:
Good box boundaries reduce accidental coupling and make future migrations far easier.
While Hive is fast for simple NoSQL document storage, production apps in 2025/2026 predominantly adopt Drift for relational databases (providing compile-time type safety and reactive auto-updating streams) or secure storage for tokens. When architecting new apps, evaluate whether relational querying and migrations (Drift) better fit your data model.
Use Hive for fast, lightweight NoSQL storage in Flutter. Backend integration connects app actions to real user data and secure business workflows.
Think in architecture, reliability, and team-level maintainability.
Define data model and access rules before writing convenience helper methods.
Security and data integrity issues can damage user trust faster than UI bugs.
Security rules are part of architecture, not optional deployment polish.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
Hive is a NoSQL key-value database written in pure Dart with very fast read/write speeds.
@HiveType(typeId: n) marks a class as a Hive type. Each field needs @HiveField(n) annotation.
build_runner generates the TypeAdapter code needed by Hive to serialize your custom class.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue