Skip to content
tezvyn:

Hive: A Fast, Local Key-Value Store for Dart

Source: pub.devMediumHow cards are made

Hive is a lightweight key-value database for Dart, acting like a persistent, super-fast Map. Use it to store simple data like user settings or cache API responses locally.

Why it exists

Hive was created to provide a simple, high-performance, pure-Dart local storage solution for Flutter and Dart apps. It fills the gap between basic key-value stores like SharedPreferences and full-fledged databases like SQLite, offering speed and object storage without native dependencies.

The mental model

Think of Hive as a persistent Dart Map that lives on the device. You open a "box" (which is like a map instance) and then use familiar put(key, value) and get(key) methods. It's designed to be so fast that you can read from a box directly in a Flutter widget's build method without causing performance issues.

How it works

Hive stores data in "boxes". Each box is a key-value store that can hold primitives, lists, maps, and custom Dart objects. To store custom objects, you must generate a TypeAdapter, which tells Hive how to convert the object to and from binary. This is usually done with a code generation package. For Flutter apps, the hive_flutter package provides widgets like ValueListenableBuilder that can listen to a box and automatically rebuild the UI when data changes.

When to use it

Use Hive for local, non-relational data storage. It's perfect for storing user settings (like a dark mode toggle), caching API responses to make your app faster and work offline, or saving simple application state that needs to persist between sessions. Its write performance significantly outperforms SharedPreferences and SQLite.

When not to use it

Avoid Hive when you need complex queries, joins, or relationships between different data objects. It is a key-value store, not a relational database. If you need to filter a large dataset by multiple properties or establish links between objects, a more advanced database like Isar (from the same authors) or SQLite would be a better choice.

One canonical example

To save a user's dark mode preference, you first open a box: await Hive.openBox('settings');. Then, you can write a value synchronously: Hive.box('settings').put('darkMode', true);. To read it back, you simply use get: bool isDarkMode = Hive.box('settings').get('darkMode', defaultValue: false);. After the initial box opening, the read and write operations are immediate and don't require await.

Interview question

For which scenario would Hive be the most appropriate local storage solution in a Flutter app?

  • a.Implementing a secure, encrypted database for sensitive financial transactions.
  • b.Storing a large catalog of products that requires frequent filtering by multiple attributes.
  • c.Managing a user's social network graph with complex relationships between friends and posts.
  • d.Persisting user preferences like dark mode and caching recent API responses for offline use.Correct
Why?

The card explicitly states Hive is "perfect for storing user settings (like a dark mode toggle), caching API responses". It also advises against using it for "complex queries, joins, or relationships", which the other options imply.

Just read this? Test yourself on what you have been reading.

Read the original → pub.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles