Poorgramer-Zack/dart-expert-skills / skills/archived/flutter-db
flutter-db
VerifiedLocal database and persistence selection for Flutter including SharedPreferences, SecureStorage, Hive, and Drift. Use when implementing offline storage, encrypted data persistence, or choosing between key-value and relational local databases.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 2.7 KB |
| references/databases-overview.md | markdown | 2.9 KB |
| references/drift.md | markdown | 6.6 KB |
| references/hive-ce.md | markdown | 6.2 KB |
| references/secure-storage.md | markdown | 3.8 KB |
| references/shared-preferences.md | markdown | 3.1 KB |
references/hive-ce.md
View on GitHub---
name: "hive-ce-best-practices"
description: "Best practices and guidelines for Hive CE Latest Version Best Practices Guide (v2.19.x). Use this skill when you need to write or review code related to Hive CE Latest Version Best Practices Guide (v2.19.x)."
metadata:
last_modified: "2026-03-12 11:18:17 (GMT+8)"
---
# Hive CE Latest Version Best Practices Guide (v2.19.x)
## Goal
`hive_ce` (Hive Community Edition) is a wildly popular, lightweight, and ultra-fast NoSQL local key-value database within the Flutter ecosystem. Because the original `hive` repository has languished without active maintenance for years, the open-source community forked it, establishing `hive_ce` to continuously support the newest formulations of Dart and Flutter.
Currently, the latest version of `hive_ce` is roughly **2.19.3**.
## Instructions
### 1. Dependencies and Configuration
Please comprehensively delete all legacy `hive` and `hive_flutter` dependencies, replacing them entirely with `hive_ce`.
```yaml
dependencies:
hive_ce: ^2.19.3
hive_ce_flutter: ^2.0.0
dev_dependencies:
hive_ce_generator: ^2.0.0
build_runner: ^2.4.0
```
### 2. Initialization Best Practices
Prior to the application's launch, Hive MUST be initialized and all utilized TypeAdapters must be registered.
```dart
import 'package:flutter/material.dart';
import 'package:hive_ce_flutter/hive_ce_flutter.dart';
void main() async {
// 1. Initialize Flutter bindings
WidgetsFlutterBinding.ensureInitialized();
// 2. Initialize Hive CE (It autonomously locates the appropriate App directory)
await Hive.initFlutter();
// 3. Register custom object Adapters
// Hive.registerAdapter(UserAdapter());
// 4. Open critically required Boxes (Best Practice: Open frequently consulted Boxes precisely at startup)
await Hive.openBox('settings');
runApp(const MyApp());
}
```
### 3. Custom Objects and Code Generation (Type Adapters)
Natively, Hive CE only supports primal primitives (String, int, bool, List, Map, etc.). If intending to store custom strongly-typed objects, you **MUST** utilize `@HiveType` and `@HiveField` along with orchestrating code generation.
```dart
import 'package:hive_ce/hive_ce.dart';
part 'user.g.dart'; // REQUIRED
@HiveType(typeId: 1) // ⚠️ typeId MUST remain absolutely unique across the entire App (0~223)
class User extends HiveObject { // It is highly recommended to inherit HiveObject guaranteeing access to .save() and .delete() utilities
@HiveField(0)
String id;
@HiveField(1)
String name;
@HiveField(2, defaultValue: 18) // Best Practice: All newly appended fields introduced later MUST be assigned a defaultValue
int age;
User({required this.id, required this.name, required this.age});
}
```
**Compilation**: Executing `dart run build_runner build -d` spins out `user.g.dart` encapsulating `UserAdapter`. Reminisce to explicitly invoke `Hive.registerAdapter(UserAdapter());` within `main()`.
### 4. Read/Write Operations and Best Practices
#### 4.1 Basic Read/Write (Synchronous Operations)
Once a Box assumes an 'opened' state, all intrinsic read and write maneuvers translate into purely **Synchronous** transactions, inherently bequeathing Hive incredibly massive performance throughputs.
```dart
final settingsBox = Hive.box('settings');
// Write functionality
settingsBox.put('theme', 'dark');
// Read functionality (Embraces default fallback values)
final theme = settingsBox.get('theme', defaultValue: 'light');
// Deletion operations
settingsBox.delete('theme');
```
#### 4.2 LazyBox (Dedicated to Gigantic Data Volumes)
If you operate a Box accumulating tens of thousands of historical log records, initially cracking it open rapidly consumes egregious memories. Instantly pivot towards using `LazyBox`. LazyBox strategically deflects from reading all values into memory simultaneously; exclusively keys are cached initially.
```dart
await Hive.openLazyBox('huge_data');
final lazyBox = Hive.lazyBox('huge_data');
// A LazyBox's `get` execution performs ASYNCHRONOUSLY!
final data = await lazyBox.get('large_key');
```
### 5. UI Layer Reactive Integrations (ValueListenableBuilder)
Hive natively incorporates a mighty event listening architecture. You can directly command UI trees to autonomically refresh reflecting database mutations traversing `ValueListenableBuilder`, blissfully absolving the requirement of incorporating alien state management packages.
```dart
import 'package:hive_ce_flutter/hive_ce_flutter.dart';
class SettingsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Audit a specific targeted Box
return ValueListenableBuilder(
valueListenable: Hive.box('settings').listenable(keys: ['theme']), // Optimization: Listen EXCLUSIVELY to the key 'theme'
builder: (context, box, widget) {
final currentTheme = box.get('theme', defaultValue: 'light');
return SwitchListTile(
title: const Text('Dark Mode'),
value: currentTheme == 'dark',
onChanged: (val) {
// Uniquely upon writing, the elevated Builder directly triggers reconstructive sequences automatically!
box.put('theme', val ? 'dark' : 'light');
},
);
},
);
}
}
```
### 6. Summary
1. **Identify the CE Edition explicitly**: Guarantee newly formulated projects strictly invoke `hive_ce` reinforcing full compliance aligning the newest Dart SDKs supplemented with sustained Bug rectifications.
2. **Forever Establish Default Values**: When broadening already deployed `@HiveType` models, modern nascent `@HiveField` entities undeniably MUST define a `defaultValue`; otherwise interacting extracting archived data inevitably triggers immediate application collapses.
3. **Exploit the `HiveObject` capabilities masterfully**: Deriving subclasses strictly off `HiveObject` guarantees direct effortless `item.save()` calls following immediate field manipulation, providing breathtaking semantic fluidity intuitively.
## Constraints
* Assure you ALWAYS execute code generation `dart run build_runner build -d` modifying ANY structural layouts containing `@HiveType` properties.
* Strictly reject applying synchronous `.box()` acquisitions probing `LazyBox` configurations preventing lethal async conversion crashes entirely.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |