How to Use Hive in Flutter: Simple Local Storage Guide
To use
Hive in Flutter, first add the hive and hive_flutter packages to your project, then initialize Hive in your main() function. Open a box to store data locally and use simple methods like put() and get() to save and retrieve data.Syntax
Hive uses boxes to store data locally. You open a box with Hive.openBox('boxName'). Use box.put(key, value) to save data and box.get(key) to read it. Initialize Hive with Hive.initFlutter() before opening boxes.
dart
import 'package:hive_flutter/hive_flutter.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Hive.initFlutter(); var box = await Hive.openBox('myBox'); // Save data await box.put('name', 'Alice'); // Read data var name = box.get('name'); print(name); // Outputs: Alice }
Output
Alice
Example
This example shows a simple Flutter app that uses Hive to save and display a counter value locally. The counter persists even after restarting the app.
dart
import 'package:flutter/material.dart'; import 'package:hive_flutter/hive_flutter.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Hive.initFlutter(); await Hive.openBox('counterBox'); runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: CounterPage(), ); } } class CounterPage extends StatefulWidget { @override _CounterPageState createState() => _CounterPageState(); } class _CounterPageState extends State<CounterPage> { final box = Hive.box('counterBox'); int get counter => box.get('counter', defaultValue: 0); void increment() { box.put('counter', counter + 1); setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Hive Counter')), body: Center( child: Text('Counter: $counter', style: TextStyle(fontSize: 24)), ), floatingActionButton: FloatingActionButton( onPressed: increment, child: Icon(Icons.add), ), ); } }
Output
A Flutter app screen showing 'Counter: 0' initially, increasing by 1 each time the + button is pressed, persisting after app restart.
Common Pitfalls
- Forgetting to call
await Hive.initFlutter()before opening boxes causes errors. - Not opening a box before using it will throw exceptions.
- Using non-primitive types without adapters will fail; Hive needs adapters for custom objects.
- Not closing boxes when done can cause resource leaks.
dart
/* Wrong: Using box before initialization */ void main() { var box = Hive.box('myBox'); // Error: box not opened yet } /* Right: Initialize and open box first */ void main() async { WidgetsFlutterBinding.ensureInitialized(); await Hive.initFlutter(); var box = await Hive.openBox('myBox'); }
Quick Reference
| Action | Code Example |
|---|---|
| Initialize Hive | await Hive.initFlutter(); |
| Open a box | var box = await Hive.openBox('boxName'); |
| Save data | await box.put('key', value); |
| Read data | var value = box.get('key'); |
| Delete data | await box.delete('key'); |
| Close box | await box.close(); |
Key Takeaways
Always initialize Hive with Hive.initFlutter() before using it.
Open a box before reading or writing data to avoid errors.
Use simple key-value pairs or register adapters for custom objects.
Remember to close boxes when they are no longer needed.
Hive stores data locally and persists it across app restarts.