Migration to Flutter Guide
Discover our battle-tested 21-step framework for a smooth and successful migration to Flutter!
Home
Glossary

Hive in Flutter

What is Hive in Flutter?

Hive is a lightweight local database for Flutter and Dart. It is a fast NoSQL key–value store designed for simple local persistence without SQL, native database engines, or complex setup.

Hive is most commonly used for caching, offline data, user preferences, and small to medium datasets that must persist between app launches.

It is important to note that Hive is currently in maintenance mode. It remains stable and usable, but it is no longer the recommended choice for new projects that require advanced querying or complex data relationships.

How does Hive work?

Hive stores data in containers called boxes. A box behaves like a persistent map, where data is written to disk and kept in memory after opening.

By default, a standard Box eagerly loads all keys and values into RAM. This makes reads extremely fast but also means memory usage grows linearly with dataset size.

Values can be primitives, collections, or custom Dart objects serialized using TypeAdapters. Data is stored in a compact binary format optimized for speed rather than flexibility.

How to use Hive in Flutter?

import 'package:hive_flutter/hive_flutter.dart';

Future<void> initHive() async {
  // Initialize Hive
  await Hive.initFlutter();

  // Open a standard in-memory-backed box
  final box = await Hive.openBox<String>('settings');

  // Write data
  await box.put('theme', 'dark');

  // Read data
  final theme = box.get('theme');

  // Compact the box to reclaim disk space after many updates
  await box.compact();

  // Close the box when no longer needed
  await box.close();
}

Boxes are often kept open for the entire app lifecycle to avoid repeated disk access.

Managing memory with LazyBox

A critical limitation of Hive is memory usage. A standard Box loads all data into memory at startup. With large datasets, this leads to slow startup times and excessive RAM consumption.

For large collections, Hive provides LazyBox. A LazyBox keeps only keys in memory and loads values from disk on demand.

LazyBox should be used whenever:

  • The dataset is large.
  • Most values are accessed infrequently.
  • Startup performance matters.

Without LazyBox, Hive does not scale safely beyond relatively small datasets.

File growth and compaction

Hive is an append-only database. When a value is updated, the new version is appended to the file instead of overwriting the old one. Over time, this causes database files to grow indefinitely, even if the logical dataset remains small.

To mitigate this, Hive provides manual compaction using box.compact(). This rewrites the file and removes obsolete entries.

In production applications, regular compaction is essential. Without it, database files can grow to hundreds of megabytes or more, leading to storage bloat and degraded performance.

Hive vs SQLite in Flutter

SQLite is a relational database with structured queries, joins, and transactions. It is suitable for complex schemas but requires more boilerplate and careful schema management.

Hive trades query power for simplicity and speed. It works best when data access is key-based and predictable.

If the application requires querying, filtering, or relational data, SQLite is usually the safer long-term choice.

Isar: the successor to Hive

Isar is created by the same author as Hive and is widely considered its spiritual successor.

While Hive excels at simple key–value storage, Isar provides:

  • rich queries
  • indexes
  • relations
  • better scalability for large datasets

For new projects that go beyond simple storage or caching, Isar is often preferred over Hive.

Hive remains useful for minimal persistence needs, but Isar is the modern choice for non-trivial local databases in Flutter.

Common mistakes when using Hive

Common issues include:

  • Using a standard Box for large datasets instead of LazyBox.
  • Forgetting to compact the database after frequent updates.
  • Treating Hive as a relational database.
  • Modifying object models without handling adapter changes.

These mistakes often lead to memory pressure, large database files, or runtime crashes.

When not use Hive

Hive becomes problematic when:

  • Datasets grow large.
  • Querying requirements increase.
  • Frequent updates cause file bloat.
  • Multiple teams evolve storage models independently.

In such cases, Isar or a relational database scales more reliably.

Learn more

Sharing Logic Across Multiple Platforms - Solution by LeanCode

Sharing Logic Across Multiple Platforms

When a project grows, sooner or later, a need for sharing logic across multiple clients and a server arises. At LeanCode, we completed a project that extensively employed logic sharing successfully. In this article, we highlight the steps and architecture decisions of the picked solution.

Flutter open source packages by LeanCode

6 Non-obvious Flutter and Dart Packages

Almost every production-grade app depends on tens of packages. Using them allows for quick implementation of standard functionalities. Take a closer look at 6 useful but not-so-popular Dart and Flutter packages made and tested by LeanCode's devs.

12 Flutter & Dart Code Hacks
by LeanCode

12 Flutter & Dart Code Hacks & Best Practices – How to Write Better Code?

In this article, we’re sharing LeanCode’s 12 practical Flutter and Dart patterns that help you write less boilerplate, make your code cleaner, and catch mistakes earlier. Apply these patterns and you'll find yourself coding faster, communicating more clearly with your teammates, and spending less time debugging issues that the compiler could have caught.