Watch the video (14 min)
On this page15 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. 1. Why a Local Database
  5. 2. What Is Hive
  6. 3. The Three Moving Parts
  7. 4. Create the Project and Add Packages
  8. 5. The Folder Layout
  9. Code Examples
  10. Common Beginner Mistakes
  11. Best Practices
  12. Mini Challenge
  13. Practice Questions
  14. Practice Answers
  15. Key Takeaways

Lecture 001 — What Is Hive & Project Setup

Category1 — Hive Basics
Lecture001 of 007
Estimated video length14 min
TargetFlutter 3.44 · Dart 3.12 · hive 2.2
LevelBeginner
VideoPart 1 — Introduction, Setup, Model Adapters & Manager Classes
Course intro videoFlutter Hive Database Full Course — what you'll learn
Source codeGitHub — urraan_hive_final_code

Every variable in your app disappears when the app closes. A database keeps data on the phone so it is still there tomorrow. Hive is a small, fast database written in Dart — no SQL, no setup files, just boxes you put things into.

Contents

Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways


Prerequisites

  • Dart classes, lists and maps; Future, async and await
  • A running Flutter app: Scaffold, AppBar, StatelessWidget (Essential Widgets 001)

Connection With Previous Lectures

In earlier courses all data lived in variables and lists. Close the app and it is gone. Real apps — notes, contacts, a to-do list — must remember. This course builds one such app: a contacts-style list where each person has a name, an address, a photo and hobbies, all saved with Hive.


Learning Objectives

By the end of this lecture you will be able to:

  • Explain what Hive is and when to choose it
  • Add hive, hive_flutter, image_picker, hive_generator and build_runner
  • Describe a box and a type adapter in one sentence each
  • Know the folder layout the whole course uses

1. Why a Local Database

Where data livesSurvives closing the app?
A variable or list❌
Hive box✅
A server (Firebase, an API)✅ — but needs internet

A local database stores data in a file inside the app's own folder. It works offline, is instant, and belongs to that one phone.

2. What Is Hive

  • Key–value storage. Like a Dart Map that is saved to disk: box.put('name', 'Zayan').
  • Boxes. A box is one named container, like a table. You open it once, then read and write it synchronously — no await on every read.
  • Dart objects. With a type adapter you store your own classes, not just text and numbers.
  • Cross-platform. Android, iOS, web, desktop.

Hive is a good fit for settings, caches, and lists of a few thousand records. For heavy relational data with joins, a SQL database (sqflite, Drift) fits better.

3. The Three Moving Parts

  1. Box — where data lives (Hive.openBox).
  2. Model + TypeAdapter — teaches Hive how to save your class (Lecture 003).
  3. Manager class — one file holding every add / update / delete call (Lecture 004).

4. Create the Project and Add Packages

bash
flutter create hive_demo
cd hive_demo
flutter pub add hive hive_flutter image_picker
flutter pub add --dev hive_generator build_runner
  • hive — the database.
  • hive_flutter — finds a safe folder on each platform (Hive.initFlutter()) and adds ValueListenableBuilder support.
  • image_picker — choose a photo (Lecture 007).
  • hive_generator + build_runner — write the type adapter for us.

5. The Folder Layout

output
lib/
  main.dart
  data_hive/
    data_objects.dart      ← the model (Lecture 003)
    data_objects.g.dart    ← generated, never edit
    general_box.dart       ← the manager (Lecture 004)
  screen_dashboard.dart    ← the list (Lecture 005)
  screen_add_update_record.dart  ← the form (Lectures 006–007)

Everything that touches the database lives in data_hive/; screens only call the manager.


Code Examples

Example 1 — Proof That Hive Is Installed

dart
// lib/main.dart
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(colorSchemeSeed: Colors.deepPurple),
      home: const Scaffold(body: Center(child: Text('Hive is ready'))),
    );
  }
}

WidgetsFlutterBinding.ensureInitialized() is required because main now awaits something before runApp. Hive.initFlutter() picks the storage folder.


Common Beginner Mistakes

1. Forgetting await Hive.initFlutter() — opening a box before this fails at run time because Hive does not know where to save.

2. Forgetting WidgetsFlutterBinding.ensureInitialized() — needed whenever main awaits plugin code before runApp.

3. Importing hive but calling initFlutter — initFlutter comes from hive_flutter. Import package:hive_flutter/hive_flutter.dart.


Best Practices

  • Initialise Hive once, in main, before runApp.
  • Keep all database code in one folder; screens should not know Hive's details.
  • Commit .g.dart files only if your team does not run build_runner in CI; never edit them.

Mini Challenge

Create a new project, add the five packages, run it, and show Hive is ready on screen. Then change the text colour to prove hot reload works.


Practice Questions

  1. What is a box? 2. Why does main need ensureInitialized? 3. Which package provides initFlutter? 4. Name one thing Hive is not good for.

Practice Answers

  1. A named container of key–value data, like a table or a persistent Map.
  2. Because main runs async code before runApp, and plugins need the Flutter engine bound.
  3. hive_flutter.
  4. Complex relational queries with joins — use a SQL database.

Key Takeaways

  • Hive saves Dart data on the device as key–value pairs inside boxes.
  • hive_flutter gives initFlutter(); hive_generator + build_runner write adapters.
  • All database code lives in lib/data_hive/.

Video lecture

Lecture 001 · 14 min