0Pricing
Flutter Mobile Development · Lección

Almacenamiento local con Hive y NoSQL

Aprenda a usar Hive, una base de datos NoSQL de tipo clave-valor rápida para Flutter, para conservar objetos estructurados localmente sin SQL.

Almacenamiento local con Hive y NoSQL es una lección gratuita de Flutter Mobile Development en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Flutter Mobile Development, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Flutter Mobile Development incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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.0

Initializing 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.

Preguntas frecuentes

¿La lección «Almacenamiento local con Hive y NoSQL» es gratis?

Sí — el texto completo de «Almacenamiento local con Hive y NoSQL» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Flutter Mobile Development, actualiza a CoddyKit PRO. El curso de Flutter Mobile Development incluye 4 lecciones en total.

¿Qué aprenderé en «Almacenamiento local con Hive y NoSQL»?

Aprenda a usar Hive, una base de datos NoSQL de tipo clave-valor rápida para Flutter, para conservar objetos estructurados localmente sin SQL. Practicas Flutter Mobile Development con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Flutter Mobile Development?

No se requiere experiencia previa. Flutter Mobile Development en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Almacenamiento local con Hive y NoSQL»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Flutter Mobile Development?

Sí. Cada lección de Flutter Mobile Development incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Shared Preferences para datos
  2. Integración de bases de datos SQLite
  3. Operaciones del sistema de archivos
  4. Almacenamiento local con Hive y NoSQL
← Volver a Flutter Mobile Development