Hive e archiviazione locale NoSQL
Impari a usare Hive, un database NoSQL key-value veloce per Flutter, per salvare localmente oggetti strutturati senza SQL.
Hive e archiviazione locale NoSQL è una lezione Flutter Mobile Development gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flutter Mobile Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flutter Mobile Development include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Hive e archiviazione locale NoSQL» è gratuita?
Sì — il testo completo di «Hive e archiviazione locale NoSQL» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flutter Mobile Development, passa a CoddyKit PRO. Il corso Flutter Mobile Development include 4 lezioni in totale.
Cosa imparerò in «Hive e archiviazione locale NoSQL»?
Impari a usare Hive, un database NoSQL key-value veloce per Flutter, per salvare localmente oggetti strutturati senza SQL. Eserciti Flutter Mobile Development con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Flutter Mobile Development?
Non è richiesta alcuna esperienza precedente. Flutter Mobile Development su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Hive e archiviazione locale NoSQL»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Flutter Mobile Development?
Sì. Ogni lezione Flutter Mobile Development include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Shared Preferences per i dati
- Integrazione di database SQLite
- Operazioni sul file system
- Hive e archiviazione locale NoSQL