Hive と NoSQL ローカルストレージ
Flutter 向けの高速なキー・バリュー型 NoSQL データベースである Hive を使い、SQL なしで構造化オブジェクトをローカルに永続化する方法を学びます。
「Hive と NoSQL ローカルストレージ」はCoddyKit上の無料Flutter Mobile Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlutter Mobile Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flutter Mobile Developmentコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「Hive と NoSQL ローカルストレージ」レッスンは無料ですか?
はい。「Hive と NoSQL ローカルストレージ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flutter Mobile Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flutter Mobile Developmentコースには全4レッスンが含まれています。
「Hive と NoSQL ローカルストレージ」で何を学びますか?
Flutter 向けの高速なキー・バリュー型 NoSQL データベースである Hive を使い、SQL なしで構造化オブジェクトをローカルに永続化する方法を学びます。 ブラウザで直接実行するハンズオンコードでFlutter Mobile Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flutter Mobile Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlutter Mobile Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Hive と NoSQL ローカルストレージ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlutter Mobile Developmentレッスンでコードを書いて実行できますか?
はい。すべてのFlutter Mobile Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- データのためのShared Preferences
- SQLiteデータベースの統合
- ファイルシステム操作
- Hive と NoSQL ローカルストレージ