Ad
Convert CSV To List> With Rows And Columns
I want to convert csv to List<List> my code bellow is working and get the file and convert it like this: [[item 1;1200;1300],[item 2;1200;1300]].
It's handle [item 1;1200;1300] as a single element I can't reach item 1 or 1200 or 1300 as alone.
I want to handle it like this List[0][0] the result will be item 1.
TextButton(
onPressed: () async {
result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['csv'],
dialogTitle: 'Choose Your File',
);
if (result != null) {
PlatformFile file = result!.files.first;
filePath = file.path!;
prvdr.openFile(filePath);
} else {}
},
child: const Text('Press Here'),
),
The Fuction
void openFile(filePath) async {
File itemsFile = File(filePath);
print('CSV to List');
final input = itemsFile.openRead();
final fields = await input.transform(utf8.decoder).transform(const CsvToListConverter()).toList();
print(fields);
for(var i = 0; i < fields.length; i++){
print(fields[i][0]);
print(fields[i][1]);
print(fields[i][2]);
}
}
My csv is like this
item 1;1200;1300
item 2;1200;1300
item 3;1200;1300
item 4;1200;1300
Ad
Answer
You should do a for loop that loops on the list and splits every element by ;
Example:
final fields = await input.transform(utf8.decoder).transform(const CsvToListConverter()).toList();
List<List<String>> finalList = [];
for(var i = 0; i < fields.length; i++){
finalList.add(fields[i].split(";"));
}
// Now you cand access finalList
for(var i = 0; i < finalFields.length; i++){
print(finalFields[i][0]);
print(finalFields[i][1]);
print(finalFields[i][2]);
}
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Octobercms/Laravel get id based on relation
- → How do i get base url in OctoberCMS?
- → Eloquent Multitable query
- → ListView.DataSource looping data for React Native
- → Laravel File Listing & Counting by Filtered File Names
- → List class element(index).fadetoggle(some speed) not working
- → Using Array in '->where()' for Laravel Query Building
- → List of react-native StyleSheet properties and options
- → How to access a complex object comming from a datatable cell in JQuery/Javascript
- → getting the correct record in Angular with a json feed and passed data
- → CasperJS - NodeList.length return 0
- → Plugin with single item ( october cms )
Ad