Module 3
Topic 2

Hive

A lightweight, blazing fast key-value database written in pure Dart — perfect for storing complex objects with ease.

Hive package
What Is Hive?

Hive is a lightweight, blazing fast key-value database written in pure Dart. It's inspired by Bitcask and designed to be simple, powerful, and intuitive .

🚀
Blazing Fast
Great performance for read/write operations
📱
Cross-Platform
Mobile, desktop, and browser support
🔒
Encryption
Strong encryption with AES-256
🎯
Simple API
Use it just like a map
🎈
No Native Dependencies
Pure Dart — works everywhere
🔋
Batteries Included
Everything you need out of the box

✅ When to Use Hive

  • Storing complex objects (custom classes)
  • Large datasets that need to be queried and cached
  • Offline-first applications
  • When you need encrypted storage
  • When you need real-time UI updates with ValueListenableBuilder
Hive vs SharedPreferences

SharedPreferences

  • Simple key-value pairs
  • Primitive types only
  • Small data (under 1MB)
  • No encryption
  • Platform-specific
  • Slower for large data

Hive

  • Key-value with complex objects
  • Any Dart object (with type adapters)
  • Large datasets
  • AES-256 encryption
  • Pure Dart (cross-platform)
  • Blazing fast

💡 Which One to Choose?

  • Use SharedPreferences – for simple app settings, user preferences, and small data
  • Use Hive – for complex data, large datasets, offline-first apps, and encrypted storage
Setting Up Hive

To use Hive, you need to add the dependency and initialize it.

Basic Usage

Hive works like a map. Here's how to use it for basic operations.

Storing Custom Objects

Hive can store any Dart object using type adapters . You need to define a model class with annotations and generate the adapter.

Hive with Flutter UI

Hive integrates beautifully with Flutter through ValueListenableBuilder , which rebuilds the UI when the data changes.

Complete Example: To-Do App with Hive

Here's a complete to-do app using Hive for persistent storage with real-time UI updates.

Step-by-Step Explanation
1.
Add dependencies – Add hive , hive_flutter , and dev dependencies for code generation.
2.
Define the model – Create a class with @HiveType and @HiveField annotations.
3.
Generate the adapter – Run build_runner to generate the .g.dart file.
4.
Initialize Hive – Call Hive.initFlutter() and register the adapter.
5.
Open a box – Use Hive.openBox<T>('boxName') to open a typed box.
6.
Use in UI – Use ValueListenableBuilder for real-time UI updates when data changes.
Best Practices

✅ Do's

  • Use type adapters for all custom objects
  • Use ValueListenableBuilder for real-time UI updates
  • Use HiveObject for objects that need save/delete methods
  • Group related data in different boxes for better organization
  • Use encryption for sensitive data
  • Call box.compact() periodically to reduce file size

❌ Don'ts

  • Don't forget to register adapters before opening boxes
  • Don't change typeId after data is stored
  • Don't store excessively large objects (consider SQLite)
  • Don't use Hive for complex queries (use SQLite)
Common Mistakes
❌ Mistake 1: Forgetting to register adapters

If you don't register the adapter, Hive won't know how to serialize/deserialize your custom objects.

✅ Correct: Always register adapters

Hive.registerAdapter(MyObjectAdapter());

❌ Mistake 2: Changing typeId after storing data

If you change a typeId , Hive won't be able to read previously stored data.

✅ Correct: Keep typeId stable

Once you assign a typeId , never change it. If you need to add new fields, just add them.

❌ Mistake 3: Not using ValueListenableBuilder

Without ValueListenableBuilder , the UI won't update when the data changes.

✅ Correct: Use ValueListenableBuilder

ValueListenableBuilder(valueListenable: box.listenable(), builder: ...)

🎯 Key Takeaway

Hive is a powerful, fast, and easy-to-use NoSQL database for Flutter. It's perfect for storing complex objects with real-time UI updates. The type adapter system makes it type-safe, and ValueListenableBuilder provides automatic UI updates. Use Hive for offline-first apps and large datasets.