모델 형태로 파싱하기 위해서는 모델 class가 필요합니다.
엑셀로 파싱하기 가장 좋은 예제가 단어장일것 같아 단어장으로 모델 데이터를 생성했고
예제는 null이 가능하도록 수정했습니다.
model.dart
class WordEntry {
final String word;
final String meaning;
final String? example;
WordEntry({
required this.word,
required this.meaning,
this.example,
});
@override
String toString() {
return 'Word: $word, Meaning: $meaning, Example: ${example ?? "N/A"}';
}
}
파싱하는 열 선택하는 변수
int? wordColumnIndex;
int? meaningColumnIndex;
int? exampleColumnIndex;
파싱하는 부분
var bytes = file!.readAsBytesSync();
var excel = Excel.decodeBytes(bytes);
var table = excel.tables[excel.tables.keys.first]!;
setState(() {
wordEntries = table.rows
.skip(1)
.map((row) {
return WordEntry(
word: row[wordColumnIndex!]?.value.toString() ?? '',
meaning: row[meaningColumnIndex!]?.value.toString() ?? '',
example: exampleColumnIndex != null
? row[exampleColumnIndex!]?.value.toString()
: null,
);
})
.where((entry) => entry.word.isNotEmpty && entry.meaning.isNotEmpty)
.toList();
});
설명을 하자면, 각 컬럼인덱스에서
선택된 컬럼으로 모델 데이터로 파싱합니다.
간단하죠?
'develop > Flutter' 카테고리의 다른 글
Dart에서 맵(map) 다루기 (0) | 2024.11.11 |
---|---|
Dart에서 리스트(List) 다루기 (0) | 2024.11.10 |
[Flutter] 하단에 고정된 스크롤 가능한 바텀시트 DraggableScrollableSheet 사용하기 (0) | 2024.07.26 |
Framework 'flutter_inappwebview' not found 6.0.0 버전 (0) | 2024.05.14 |
매일 사용하는 flutter 명령어 (0) | 2024.05.13 |