0
0
Fluttermobile~5 mins

Hive for NoSQL storage in Flutter

Choose your learning style9 modes available
Introduction

Hive is a simple way to save and get data on your device without using complex databases. It helps apps remember things even after closing.

You want to save user settings like theme or language.
You need to store small lists of items like favorite songs or notes.
You want to keep data offline so the app works without internet.
You want a fast and easy way to save data without a big setup.
Syntax
Flutter
var box = await Hive.openBox('boxName');

// To save data
await box.put('key', value);

// To read data
var value = box.get('key');
Use openBox to create or open a storage container called a box.
Use put to save data with a key, and get to read it back.
Examples
This example saves a setting for dark mode and reads it back.
Flutter
var box = await Hive.openBox('settings');
await box.put('darkMode', true);
bool darkMode = box.get('darkMode');
This example saves a favorite song and retrieves it.
Flutter
var box = await Hive.openBox('favorites');
await box.put('song1', 'Imagine');
String song = box.get('song1');
Sample App

This program opens a box called 'myBox', saves the name 'Alice', then reads and prints it.

Flutter
import 'package:hive/hive.dart';

void main() async {
  var box = await Hive.openBox('myBox');

  // Save data
  await box.put('name', 'Alice');

  // Read data
  var name = box.get('name');
  print(name);
}
OutputSuccess
Important Notes

Hive stores data locally on the device, so it works offline.

Data is saved as key-value pairs, like a dictionary.

Remember to await async calls like openBox and put to avoid errors.

Summary

Hive is an easy local storage for Flutter apps.

Use boxes to save and get data with keys.

Great for small data like settings or favorites.