중급
Persistence
키-값 저장, 파일 저장, SQLite로 기기 안에 데이터를 보관합니다.
핵심 개념
Persistence 주요 항목
저장 방식 선택
작은 설정값은 key-value 저장소가 적합합니다.
언제 쓰나: 파일 저장은 이미지, 문서, 캐시 파일처럼 파일 단위 데이터에 맞습니다.
Flutter에서: SQLite는 구조화된 데이터, 검색, 관계가 필요한 경우에 사용합니다.
final settings = <String, Object>{
'theme': 'dark',
'fontSize': 16,
'cachedAt': DateTime.now().toIso8601String(),
};
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: const Color(0xFFDCE6EF)),
borderRadius: BorderRadius.circular(8),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
decoration: BoxDecoration(
color: const Color(0xFFEEF7FB),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'실행 결과',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w900),
),
),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
Chip(label: Text('UI')),
Chip(label: Text('상태')),
Chip(label: Text('데이터'))
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('코드가 앱의 한 부분으로 연결됩니다.')
],
)
],
),
)
앱 화면 예시
데이터상태위젯
코드에서 만든 값을 위젯에 전달해 화면에 표시합니다.
운영 기준
민감한 정보는 일반 저장소에 평문으로 저장하지 않습니다.
언제 쓰나: 캐시 데이터와 영구 데이터의 삭제 정책을 다르게 잡습니다.
Flutter에서: 오프라인 지원을 한다면 서버와 로컬 데이터의 동기화 규칙이 필요합니다.
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text('운영 기준', style: TextStyle(fontWeight: FontWeight.bold)),
SizedBox(height: 8),
Text('민감한 정보는 일반 저장소에 평문으로 저장하지 않습니다.'),
],
),
),
)
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: const Color(0xFFDCE6EF)),
borderRadius: BorderRadius.circular(8),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
decoration: BoxDecoration(
color: const Color(0xFFEEF7FB),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'운영 기준',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w900),
),
)
],
),
)
운영 기준
확장 개념
저장 방식 선택
- 설정값과 토큰처럼 작은 값은 key-value 저장소가 적합합니다.
- 파일 자체를 저장하거나 읽어야 하면 path_provider와 File API를 사용합니다.
- 검색, 정렬, 관계가 필요한 구조화 데이터는 SQLite 계열 저장소를 검토합니다.
코드
예제
// 설정값: key-value storage
// 파일: path_provider + File
// 구조화 데이터: SQLite
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: const Color(0xFFDCE6EF)),
borderRadius: BorderRadius.circular(8),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
decoration: BoxDecoration(
color: const Color(0xFFEEF7FB),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'실행 결과',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w900),
),
),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
Chip(label: Text('UI')),
Chip(label: Text('상태')),
Chip(label: Text('데이터'))
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('코드가 앱의 한 부분으로 연결됩니다.')
],
)
],
),
)
저장 방식 선택
설정값파일SQLite
데이터 성격에 맞춰 저장소를 고릅니다.
다음 단계
실습 체크리스트
저장 대상 분류보안 필요성 확인캐시 만료 정책 정리
- 출처세부 기준과 최신 변경 사항을 확인할 수 있습니다.
- Flutter API Reference클래스, 메서드, 생성자 세부 정의를 확인합니다.
- Data & backend네트워크, 저장소, 직렬화, Firebase 문서를 함께 봅니다.