๊ธฐ๋ฅ : ํ๋ฉด์์ ํน์ ๋ฒํผ์ ๋๋ฅด๋ฉด csv ํ์ผ์ ์ฝ์ด์จ๋ค.
1. pub csv ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
https://pub.dev/packages/csv/install
flutter pub add csv
pubspec.yaml ํ์ผ์ ์๋์ ๊ฐ์ด ๋ฑ๋ก๋จ์ ํ์ธ
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
csv: ^5.0.2
2. assets ๋๋ ํ ๋ฆฌ ์์ฑ ํ, ํ์ผ ๋ฑ๋ก
๋ฃจํธ๋๋ ํ ๋ฆฌ ์๋์ assets ๋๋ ํ ๋ฆฌ > csv ๋๋ ํ ๋ฆฌ ์์ฑ.
์ฌ์ฉํ๋ ค๋ csv ํ์ผ์ ๋ฑ๋ก
์ฐธ๊ณ ๋ก test.csv ํ์ผ์ ๋ด์ฉ์ ์๋์ ๊ฐ๋ค.
3. pubspec.yaml ํ์ผ์ assets/csv/test.csv ๊ฒฝ๋ก ์ ๋ ฅ
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- assets/csv/test.csv
4. ๋ฒํผ ํด๋ฆญ์ ํ์ผ ์ฝ์ด์ค๋ ํจ์ ์ถ๊ฐ
import 'package:csv/csv.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:uddutsi/tab/search/upload_model.dart';
class SearchPage extends StatefulWidget {
const SearchPage({Key? key}) : super(key: key);
@override
State<SearchPage> createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
List<List<dynamic>> _data = [];
//csv ํ์ผ ๋ก๋ํ๋ ํจ์ ์ ์ธ
void _loadCSV() async{
final _rawData = await rootBundle.loadString('assets/csv/test.csv');
List<List<dynamic>> _listData =
const CsvToListConverter().convert(_rawData);
setState(() {
_data = _listData;
});
}
@override
Widget build(BuildContext context) {
UploadModel model = UploadModel();
return Scaffold(
appBar: AppBar(
title: const Text('[test]'),
),
body: Row(
children: [
ElevatedButton(
onPressed: _loadCSV ,
child: const Text('upload categories'),
),
],
)
);
}
}
upload categories ๋ฒํผ์ ํด๋ฆญํ์๋(๋๋ฒ๊น ๋ชจ๋๋ก ์คํ์ค) ์๋์ฒ๋ผ ๋ฐ์ดํฐ๊ฐ _listData ๋ณ์์ ๋ค์ด๊ฐ ์์์ ํ์ธํ ์ ์๋ค.
๋๊ธ