On this page12 sections
Lecture 004 — The Manager Class
| Category | 2 — Models & Manager |
| Lecture | 004 of 007 |
| Estimated video length | 18 min |
| Target | Flutter 3.44 · Dart 3.12 · hive 2.2 |
| Level | Beginner |
| Video | Part 1 — Introduction, Setup, Model Adapters & Manager Classes |
| Source code | GitHub — urraan_hive_final_code |
Screens should not know how Hive works. We put every database call in one class —
GeneralBox— so a screen only says "save this" or "delete that".
Contents
Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways
Prerequisites
Lectures 001–003: Box, put/delete, and the generated DataObjectsAdapter.
Connection With Previous Lectures
We have a model and an adapter. Someone must register the adapter, open the box, and offer add / update / delete. That someone is the manager.
Learning Objectives
- Write an
init()that starts Hive, registers the adapter and opens the box - Write
addUpdate,deleteandgetUniqueKey - Call
init()frommainbeforerunApp
1. The Manager
// lib/data_hive/general_box.dart
import 'package:hive_flutter/hive_flutter.dart';
import 'data_objects.dart';
class GeneralBox {
static const String dataObjects = 'DataObjects';
static Future<void> init() async {
await Hive.initFlutter();
if (!Hive.isAdapterRegistered(DataObjectsAdapter().typeId)) {
Hive.registerAdapter(DataObjectsAdapter());
}
if (!Hive.isBoxOpen(dataObjects)) {
await Hive.openBox<DataObjects>(dataObjects);
}
}
static Box<DataObjects> getBox() => Hive.box<DataObjects>(dataObjects);
static String getUniqueKey() =>
DateTime.now().millisecondsSinceEpoch.toString();
static bool addUpdate(DataObjects item) {
final box = getBox();
box.put(item.key, item);
return box.containsKey(item.key);
}
static bool delete(String key) {
final box = getBox();
box.delete(key);
return !box.containsKey(key);
}
static List<DataObjects> getAll() => getBox().values.toList();
}static— every member is static; screens callGeneralBox.addUpdate(...)without creating an object.init()— initialise, register, open, in that order. The twoifchecks make it safe to call twice (for example after a hot restart).getUniqueKey()— milliseconds since 1970 as text: unique enough for one user adding records by hand.addUpdate—putcreates the record if the key is new and replaces it if the key exists, so one method does both. It returnstruewhen the key is present afterwards, so the screen can react to success or failure.delete— same idea,trueonce the key is gone.
2. Call It From main
// lib/main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await GeneralBox.init();
runApp(const MyApp());
}The box must be open before any widget calls getBox().
Code Examples
Example 1 — Using the Manager
final person = DataObjects()
..key = GeneralBox.getUniqueKey()
..name = 'Zayan'
..address = 'Karachi'
..hobbies = ['Cricket'];
final saved = GeneralBox.addUpdate(person); // true
GeneralBox.delete(person.key); // trueThe .. cascade sets several fields on the new object in one expression.
Common Beginner Mistakes
1. Calling init() without await — the app starts before the box is open and the first
getBox() throws "Box not found".
2. Creating a new key on every update — updating must reuse the existing item.key,
otherwise you create a copy instead of replacing. Lecture 006 handles this.
3. Opening the box with one type and reading with another — openBox<DataObjects> needs
Hive.box<DataObjects>.
Best Practices
- One manager (or one per box) — never scatter
Hive.box(...)through screens. - Keep box names in constants.
- Return a success
boolso the UI can show a message.
Mini Challenge
Add static int count() => getBox().length; and a clearAll() that empties the box.
Practice Questions
- Why are the manager's members
static? 2. What doesisAdapterRegisteredprevent? - Why is
addUpdateone method instead of two? 4. What mustmaindo beforerunApp?
Practice Answers
- So no object is needed — one shared box.
- Registering the same adapter twice (an error).
putreplaces an existing key or adds a new one.await GeneralBox.init().
Key Takeaways
- The manager owns Hive: init, register, open, add/update, delete.
putwith the same key is an update.mainawaitsinit()beforerunApp.