On this page16 sections
- Prerequisites
- Connection With Previous Lectures
- Learning Objectives
- 1. Why a Local Database
- 2. What Is Hive
- 3. The Three Moving Parts
- 4. Create the Project and Add Packages
- 5. The Folder Layout
- Code Examples
- Common Beginner Mistakes
- Best Practices
- Mini Challenge
- Practice Questions
- Practice Answers
- Key Takeaways
- Full Code (Copy-Paste Reference)
Lecture 001: What Is Hive & Project Setup
| Category | 1: Hive Basics |
| Lecture | 001 of 007 |
| Estimated video length | 14 min |
| Target | Flutter 3.44 · Dart 3.12 · hive 2.2 |
| Level | Beginner |
| Video | Part 1: Introduction, Setup, Model Adapters & Manager Classes |
| Course intro video | Flutter Hive Database Full Course: what you'll learn |
| Source code | GitHub: 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 · Full Code
Prerequisites
- Dart classes, lists and maps;
Future,asyncandawait - 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_generatorandbuild_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 lives | Survives 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
Mapthat 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
awaiton 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
- Box: where data lives (
Hive.openBox). - Model + TypeAdapter: teaches Hive how to save your class (Lecture 003).
- Manager class: one file holding every add / update / delete call (Lecture 004).
4. Create the Project and Add Packages
flutter create hive_demo
cd hive_demo
flutter pub add hive hive_flutter image_picker
flutter pub add --dev hive_generator build_runnerhive: the database.hive_flutter: finds a safe folder on each platform (Hive.initFlutter()) and addsValueListenableBuildersupport.image_picker: choose a photo (Lecture 007).hive_generator+build_runner: write the type adapter for us.
5. The Folder Layout
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
// 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, beforerunApp. - Keep all database code in one folder; screens should not know Hive's details.
- Commit
.g.dartfiles only if your team does not runbuild_runnerin 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
- What is a box? 2. Why does
mainneedensureInitialized? 3. Which package providesinitFlutter? 4. Name one thing Hive is not good for.
Practice Answers
- A named container of key–value data, like a table or a persistent
Map. - Because
mainruns async code beforerunApp, and plugins need the Flutter engine bound. hive_flutter.- 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_fluttergivesinitFlutter();hive_generator+build_runnerwrite adapters.- All database code lives in
lib/data_hive/.
Full Code (Copy-Paste Reference)
Everything the app needs up to and including this lecture. Just one file so far.
lib/main.dart
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
Future<void> main() async {
// Required whenever main() awaits plugin code before runApp().
WidgetsFlutterBinding.ensureInitialized();
// Finds a safe storage folder on this platform and gets Hive ready to open boxes.
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),
// Just proves Hive initialised without crashing; real boxes arrive in Lecture 002.
home: const Scaffold(body: Center(child: Text('Hive is ready'))),
);
}
}