path provider
ํ๋ฌํฐ ์ฑ์์ ํ์ผ ์ ์ฅ ์์น๋ฅผ ์ฒ๋ฆฌํ ์ ์๊ฒ ์ง์ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
์ฃผ์ ์ ์ฅ ์์น
์ ์ฅ ์์น | ์ค๋ช
|
Temporary directory | ์บ์๊ฐ์ด ์์๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ณต๊ฐ์ด๊ณ ์ธ์ ๋ ์ง ์ญ์ ๋ ์ ์์ต๋๋ค. |
Documents directory | ํด๋น ์ฑ๋ง์ ์ ์ฅ๊ณต๊ฐ์ด๋ฉฐ ์ฑ์ด ์ญ์ ๋๋ฉด ์์ด์ง๋๋ค. |
Downloads directory | ๋ค์ด๋ก๋ํ ํ์ผ์ ์ ์ฅํ๋ ๊ณต๊ฐ์
๋๋ค. |
Application Support directory | ์ฑ์ ์ค์ ์ด๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ณต๊ฐ์
๋๋ค. |
์ฃผ์ ๋ฉ์๋
๋ฉ์๋ | ๋ฐํ๊ฐ | ์ค๋ช
|
getTemporaryDirectory() | Directory | Temporary directory๋ฅผ ๋ฐํํฉ๋๋ค. |
getApplicationDocumentsDirectory() | Directory | Documents directory๋ฅผ ๋ฐํํฉ๋๋ค. |
getDownloadsDirectory() | Directory | Downloads directory๋ฅผ ๋ฐํํฉ๋๋ค. |
getApplicationSupportDirectory() | Directory | Application Support directory๋ฅผ ๋ฐํํฉ๋๋ค. |
์์์ฝ๋
import 'package:path_provider/path_provider.dart';
void main() async {
// Temporary directory๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
Directory temporaryDirectory = await getTemporaryDirectory();
print(temporaryDirectory.path); // /data/user/0/com.example.myapp/cache
// Documents directory๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
Directory documentsDirectory = await getApplicationDocumentsDirectory();
print(documentsDirectory.path); // /data/user/0/com.example.myapp/documents
// Downloads directory๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
Directory downloadsDirectory = await getDownloadsDirectory();
print(downloadsDirectory.path); // /storage/emulated/0/Download
// Application Support directory๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
Directory applicationSupportDirectory =
await getApplicationSupportDirectory();
print(applicationSupportDirectory.path); // /data/user/0/com.example.myapp/shared_prefs
}
Dart
๋ณต์ฌ
์นด๋ ๋ฉ๋ชจ ์ฑ ๋ง๋ค๊ธฐ
๊ฐ๋จํ List ๋ฐ์ดํฐ๋ฅผ txt ํ์ผ๋ก ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๊ธฐ
์ฃผ์ ๊ธฐ๋ฅ
โข
๋ฉ๋ชจ ์นด๋ ์์ฑ: ๋ฉ๋ชจ ์
๋ ฅ ํ, ํ๋กํ
๋ฒํผ ํด๋ฆญ
โข
๋ฉ๋ชจ ์นด๋ ์ญ์ : ๋ฉ๋ชจ ์นด๋ ๊ธธ๊ฒ ๋๋ฅด๊ธฐ
pubspce.yaml
name: shared_data_app
description: "A new Flutter project."
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: '>=3.2.3 <4.0.0'
dependencies:
flutter:
sdk: flutter
# ๊ณต์ ํ๊ฒฝ์ค์
shared_preferences: ^2.2.2
# ํ์ผ
path_provider: ^2.1.1
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
assets:
- repo/
YAML
๋ณต์ฌ
main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
// SharedPrefences ๊ณต์ ํ๊ฒฝ์ค์ ์ ๋ฐ์ดํฐ ์ ์ฅ ์์
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home:
const FileApp(),
);
}
}
class FileApp extends StatefulWidget {
const FileApp({super.key});
State<FileApp> createState() => _FileAppState();
}
class _FileAppState extends State<FileApp> {
TextEditingController _controller = TextEditingController();
List<String> itemList = [];
void initState() {
super.initState();
initData();
}
void initData() async {
List<String> savedItemList = await readListFile();
setState(() {
itemList = savedItemList;
});
}
// ํ์ผ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ํจ์
// ๐ โก List
Future<List<String>> readListFile() async {
List<String> itemList = [];
// ์ต์ด ํ์ผ ์์ฑ
// - ์ฒ์ ํ์ผ ์์ฑ ์, SharedPeferences ๋ก 'frist' ๋ผ๋ ๋ฐ์ดํฐ๋ฅผ ํ์ธ
var key = 'first';
SharedPreferences pref = await SharedPreferences.getInstance();
var firstCheck = pref.getBool(key);
var dir = await getApplicationDocumentsDirectory();
var file;
bool fileExist = await File(dir.path + '/test.txt').exists();
// ์ต์ด์ธ ๊ฒฝ์ฐ
// 1. null 2. false
if( firstCheck == null || firstCheck == false || fileExist == false ) {
pref.setBool(key, true);
// ์ต์ด ํ์ผ ์์ฑ
// - ํ๋ก์ ํธ ์์ ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
file = await DefaultAssetBundle.of(context).loadString('repo/test.txt');
// - ๊ฐ์ ธ์จ ํ์ผ๋ก ์ค๋งํธํฐ์ ์ ์ฅํ๊ธฐ
File(dir.path + '/test.txt').writeAsStringSync(file);
} else {
// ํ์ผ ์ฝ๊ธฐ
// File(ํ์ผ๊ฒฝ๋ก)
file = await File(dir.path + '/test.txt').readAsString();
}
var array = file.split('\n'); // \n (์ํฐ)
for (var item in array) {
itemList.add(item);
}
return itemList;
}
// ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ํจ์
void writeListFile(String data) async {
// ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
var dir = await getApplicationDocumentsDirectory();
var file = await File(dir.path + '/test.txt').readAsString();
// ๊ธฐ์กด ํ์ผ์ ์ ๋ฐ์ดํฐ ์ถ๊ฐ
file = file + '\n' + data;
// ํ์ผ ์ ์ฅ
File(dir.path + '/test.txt').writeAsStringSync(file);
}
// ํ์ผ ๋ฐ์ดํฐ๋ฅผ ๊ฐฑ์ ํ๋ ํจ์
Future<bool> deleteListFile(int index) async {
// itemList ์์ index ์ ํด๋นํ๋ ๋ฐ์ดํฐ ์ญ์
List<String> copyList = [];
copyList.addAll(itemList);
copyList.removeAt(index);
// copyList ์ ๋ฐ์ดํฐ๋ค์ '\n' ๋ก ๊ตฌ๋ถํ์ฌ ๋ฌธ์์ด๋ก ๋ณํ
var fileData = "";
for (var item in copyList) {
fileData += item + "\n";
}
// ํ์ผ ์ ์ฅ
try {
var dir = await getApplicationDocumentsDirectory();
File(dir.path + '/test.txt').writeAsStringSync(fileData);
} catch (e) {
print(e);
return false;
}
return true;
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('File App'),),
body: Container(
child: Center(
child: Column(
children: [
// TextFied : controller, keyboardType
Padding(
padding: EdgeInsets.all(20.0),
child: TextField(
controller: _controller,
keyboardType: TextInputType.text,
onSubmitted: (data) {
print(data);
writeListFile(data);
setState(() {
itemList.add(data);
});
_controller.text = '';
},
),
),
const SizedBox(height: 10.0,),
Expanded(
child: ListView.builder(
itemBuilder: (context, index) {
return
GestureDetector(
onLongPress: () async {
// ๊ธธ๊ฒ ๋๋ฅธ ์นด๋ ์ญ์
bool check = await deleteListFile(index);
if( check ) {
setState(() {
itemList.removeAt(index);
});
}
},
child: Card(
child: Center(
child: Text(
itemList[index],
style: TextStyle(fontSize: 20.0),
),
),
),
);
},
itemCount: itemList.length,
)
),
],
)
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
writeListFile(_controller.text);
setState(() {
itemList.add(_controller.text);
});
_controller.text = '';
},
child: Icon(Icons.create),
),
);
}
}
Dart
๋ณต์ฌ