728x90
Flutter Hive_ce & Hive_ce_flutter 소개 및 사용법
1. Hive_ce & Hive_ce_flutter란?
hive_ce와 hive_ce_flutter는 Flutter에서 사용 가능한 경량 로컬 데이터베이스입니다.
특히 Hive_ce는 NoSQL 방식으로 데이터를 저장하며, 빠른 속도와 쉬운 사용법이 장점입니다.
📌 주요 특징:
- Key-Value 기반의 로컬 데이터 저장
- SQL 없이 사용 가능 (NoSQL)
- Flutter와 완벽한 호환 (hive_ce_flutter 사용)
- 파일 기반 스토리지 (데이터가 파일로 저장됨)
- 안드로이드 & iOS 지원
- Flutter 웹에서도 사용 가능
2. Hive_ce & Hive_ce_flutter 설치
flutter pub add hive_ce hive_ce_flutter
📌 Hive 초기화 (필수)
Flutter 앱에서 Hive를 사용하려면 먼저 초기화해야 합니다.
void main() async {
// hive_flutter initialization
await Hive.initFlutter();
runApp(const MyApp());
}
3. Hive_ce 기본 사용법
1️⃣ Box 열기 (데이터 저장 공간)
Hive에서는 Box를 이용하여 데이터를 저장합니다.
// open hive
await Hive.openBox('myBox');
2️⃣ 데이터 저장 (Key-Value)
// get the box
final box = Hive.box('myBox');
// list of todos
List todos = [];
setState(() {
todos.add(text);
textController.clear();
box.put('todos', todos);
});
3️⃣ 데이터 읽기 & 데이터 삭제
ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
final todo = todos[index];
return ListTile(
title: Text(todo),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
todos.removeAt(index);
box.put('todos', todos);
});
},
),
);
},
),
3. 실제 간단 사용 예제
main.dart
//-- main.dart
import 'package:flutter/material.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
import 'package:local_database/home_page.dart';
void main() async {
// hive_flutter initialization
await Hive.initFlutter();
// open hive
await Hive.openBox('myBox');
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false, home: HomePage());
}
}
home_page.dart
//-- home_page.dart
import 'package:flutter/material.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
// get the box
final box = Hive.box('myBox');
// text controller
final textController = TextEditingController();
// list of todos
List todos = [];
@override
void initState() {
super.initState();
// get the todos from the box
todos = box.get('todos', defaultValue: []);
}
// open new todo dialog
void openNewTodo() {
showDialog(
context: context,
builder:
(context) => AlertDialog(
title: Text('Add Test'),
content: TextField(controller: textController),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
textController.clear();
},
child: Text('취소'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
addToto();
},
child: Text('등록'),
),
],
),
);
}
// addToto
void addToto() {
String text = textController.text;
if (text.isNotEmpty) {
setState(() {
todos.add(text);
textController.clear();
box.put('todos', todos);
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: openNewTodo,
child: Icon(Icons.add),
),
body: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
final todo = todos[index];
return ListTile(
title: Text(todo),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
todos.removeAt(index);
box.put('todos', todos);
});
},
),
);
},
),
);
}
}
✔️ hive_ce와 hive_ce_flutter는 빠르고 간단한 NoSQL 로컬 데이터베이스
✔️ Box를 사용하여 데이터를 Key-Value 형태로 저장
✔️ Flutter UI에서도 쉽게 연동 가능
✔️ 앱을 종료해도 데이터 유지됨
LIST
'Develop > Flutter' 카테고리의 다른 글
[Flutter] Google Maps API 설정 및 사용 방법 (Android & iOS) (1) | 2025.03.13 |
---|---|
[Flutter] Expanded 위젯 설명 및 사용 방법 (1) | 2025.03.06 |
[Flutter] 밑에서 올라오는 팝업 위젯 (Bottom Sheet) (1) | 2025.03.04 |
[Flutter] 커스텀 버튼들 클래스를 만들어 재사용하는 방법 (1) | 2025.02.28 |
[Flutter] PNG 이미지를 버튼으로 추가하는 방법 (0) | 2025.02.28 |