HiveList and lazy box
- Flutter course
- Advanced
- Lesson 14/69
- 4 min
Handle large data with LazyBox and HiveList.
What is a LazyBox?
A LazyBox loads values from disk only when they are requested. A regular box loads all values into memory on open; a LazyBox reads them when you call await box.get('key'). This saves memory for large datasets.
final box = await Hive.openLazyBox('large');
final value = await box.get('item1');HiveList
HiveList is a special list type that holds references to objects in other boxes. Instead of copying an object into multiple boxes, you reference it with HiveList. This prevents data duplication and creates relationships between boxes.
// HiveList holds references to objects in boxes
// Creates relationships without duplicating dataPractice Use a LazyBox
Open a lazy box named 'large'. Put 'değer' under key 'item1'. Read the value with await and print it. Close the box.
Starter code
import 'package:hive/hive.dart';
void main() async {
Hive.init('hive_data');
// 'large' adında lazy box aç
// 'item1' anahtarına 'değer' koy
// değeri await ile oku ve yazdır
}Sample solution
import 'package:hive/hive.dart';
void main() async {
Hive.init('hive_data');
final box = await Hive.openLazyBox('large');
await box.put('item1', 'değer');
final value = await box.get('item1');
print(value);
await box.close();
}Expected output
değer
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the main difference between LazyBox and Box?
- LazyBox holds only strings, Box holds any type
- Box works only in memory, LazyBox on disk
- They are completely identical
- LazyBox loads values on demand, Box loads all into memory Correct answer
Explanation: Box loads all values into memory on open; LazyBox reads from disk when get is called.
2 Open a lazy box
final box = await Hive.____('large_data');
final box = await Hive.openLazyBox('large_data');
Explanation: To open a LazyBox you use Hive.openLazyBox; values are loaded on demand.
3 LazyBox and HiveList parts
| Term | Meaning |
|---|---|
LazyBox |
Loads on demand |
openLazyBox |
Opens a lazy box |
HiveList |
Cross-box reference |
await box.get |
Async value read |