Understanding Hive in Flutter || Building a Persistent Shopping List
This Blog is about how to use Hive in a Flutter app to persist data locally by building a simple shopping list.
In this guide, we’ll understand how apps store data locally using Hive in Flutter. We’ll build a simple shopping list app to learn how data can persist even after the app is closed.
By the end, you’ll understand:
- What Hive is
- How key–value storage works
- How to add, delete, and persist data
What is the Problem we are trying to solve?
Some apps work even when there is no internet.
For example, Notes apps, To-Do lists, etc.
You can add notes or tasks, and they stay there even after you close the app. To achieve this, apps need to store data locally on your device.
What is Hive?
Hive is a local database used in Flutter apps.
Think of it as a box that holds the user's data on their device.Data is stored in key-value pairs, similar to a dictionary/Objects.

Setting up Hive in a Flutter App
- Add Hive dependencies to your
pubspec.yamlfile :
Run flutter pub get to install the packages.
dependencies: hive: ^2.2.3 hive_flutter: ^1.1.0 - Import Hive and initialize it in your
main.dartfile:
- hive_flutter package gives the file system path to store the data.
- Initialize Hive before running the app.
WidgetsFlutterBinding.ensureInitialized()ensures that Flutter is fully initialized before we use Hive. If you want to study 5-6 hours a day, you need to prepare your study space first.So before starting the app, we need to set up Hive.- We are using
asyncandawaitbecause initializing Hive is an asynchronous operation. Which means it might take some time to complete, and we don't want to block the app from starting while we wait for it to finish.
import 'package:flutter/material.dart'; import 'package:hive_flutter/hive_flutter.dart'; void main() async { await WidgetsFlutterBinding.ensureInitialized(); await Hive.initFlutter(); runApp(MyApp()); } Create a Box to Store Data
- A Box in Hive is like a container that holds your data.
- You can think of it as a folder where you keep related files together.
- Open a Box in your
main.dartfile before running the app:
void main() async { await WidgetsFlutterBinding.ensureInitialized(); await Hive.initFlutter(); await Hive.openBox('shoppingList'); runApp(MyApp()); } Adding and Retrieving Data
- To add an item to the shopping list, use the
putmethod.
putmethod takes two arguments: a key and a value.- The key is a unique identifier for the item (like 'item1').
- The value is the actual item you want to store (like 'Apples').
var box = Hive.box('shoppingList'); box.put('item1', 'Apples'); - To retrieve an item from the shopping list, use the
getmethod.
var box = Hive.box('shoppingList'); var item = box.get('item1'); // item will be 'Apples' Deleting Data
- To delete an item from the shopping list, use the
deletemethod.
var box = Hive.box('shoppingList'); box.delete('item1'); // Deletes the item with key 'item1' Shopping List App Example
Here is a simple example of a shopping list app using Hive:

App Flow

When the app starts, Hive is initialized and a storage box (shoppingBox) is opened before the UI loads. As soon as the app is ready, it tries to read a saved shopping list (myList) from Hive; if this is the first time the app is opened, it simply starts with an empty list. When a user enters an item and submits it, the item is added to the in-memory list (shoppingList), and the entire updated list is immediately saved back to Hive. If the user deletes an item, it is removed from the list and the updated list is saved again. Because Hive always stores the latest version of the shopping list, the data remains available even after the app is closed and reopened, allowing the app to work fully offline.
Retrieving Data
- When the app starts, it retrieves the saved shopping list from Hive and displays it.If there is no saved list, it starts with an empty list.

Adding Items
- As soon as the user adds an item, it is appended to the list and saved to Hive.

Deleting Items
- When user clicks the delete Icon next to an item, it is removed from the list and the updated list is saved to Hive.

Recommended for you
Jan 25, 2026
Setting Up Android App widget in Flutter.
In this blog post, I will guide you through the steps to set up home_widget in your Flutter application, enabling you to create interactive home screen widgets for Android.
Jan 23, 2026
How to Use the Animations Package in Flutter
Smooth animations in Flutter can feel confusing at first. In this post, I explain how I implemented a FAB to full-screen transition using the animations package.