Hive & NoSQL Local Storage
Learn to use Hive, a fast key-value NoSQL database for Flutter, to persist structured objects locally without SQL.
Hive & NoSQL Local Storage is a free Flutter Mobile Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flutter Mobile Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Hive?
Hive is a lightweight, pure-Dart key-value database. It is great when SQLite feels heavy and SharedPreferences feels too simple.
- Very fast reads and writes
- No native dependencies
- Stores Dart objects directly
Adding Hive
Add the packages to pubspec.yaml: hive, hive_flutter, and dev dependencies hive_generator and build_runner.
dependencies:
hive: ^2.2.3
hive_flutter: ^1.1.0
dev_dependencies:
hive_generator: ^2.0.1
build_runner: ^2.4.0Initializing Hive
Initialize Hive before runApp so the database directory is ready.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
runApp(const MyApp());
}Boxes
Data lives in a Box — think of it as a typed map persisted to disk. Open one before use.
final box = await Hive.openBox('settings');Reading & Writing
Use put and get like a map. Values survive app restarts.
box.put('darkMode', true);
final isDark = box.get('darkMode', defaultValue: false);Storing Custom Objects
To store your own classes, annotate them with @HiveType and each field with @HiveField.
@HiveType(typeId: 0)
class Note extends HiveObject {
@HiveField(0)
String title;
@HiveField(1)
String body;
Note(this.title, this.body);
}Generating Adapters
Run the code generator to create the type adapter, then register it.
// terminal:
// flutter pub run build_runner build
Hive.registerAdapter(NoteAdapter());Working with a Typed Box
Open a box typed to your class to store and retrieve objects directly.
final notes = await Hive.openBox<Note>('notes');
notes.add(Note('Shopping', 'Milk and eggs'));
final first = notes.getAt(0);Updating & Deleting
Because a HiveObject remembers its box, you can call save and delete on the object itself.
final note = notes.getAt(0)!;
note.title = 'Updated';
note.save();
note.delete();Reactive UI with ValueListenable
Hive boxes expose a listenable, so ValueListenableBuilder rebuilds the UI when data changes.
ValueListenableBuilder(
valueListenable: notes.listenable(),
builder: (context, Box<Note> box, _) {
return Text('Notes: ' + box.length.toString());
},
);Hive vs SQLite
Choose Hive for simple object/key-value storage and speed. Choose SQLite when you need complex relational queries, joins or aggregations.
Quick Check
What must you do before storing a custom class in a Hive box?
Recap
You learned Hive for local NoSQL storage:
- initFlutter and boxes
- put/get key-value access
- Custom objects with generated adapters
- Reactive UI via box listenables
Hive gives you fast, structured persistence without SQL.
Frequently asked questions
Is the “Hive & NoSQL Local Storage” lesson free?
Yes — the full text of “Hive & NoSQL Local Storage” is free to read here on the web, and the Flutter Mobile Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flutter Mobile Development course, upgrade to CoddyKit PRO.
What will I learn in “Hive & NoSQL Local Storage”?
Learn to use Hive, a fast key-value NoSQL database for Flutter, to persist structured objects locally without SQL. You practise Flutter Mobile Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flutter Mobile Development?
No prior experience is required. Flutter Mobile Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Hive & NoSQL Local Storage” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flutter Mobile Development lesson?
Yes. Every Flutter Mobile Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Shared Preferences for Data
- SQLite Database Integration
- File System Operations
- Hive & NoSQL Local Storage