recently i develop an ToDo application using flutter and sqflite , for backup my data i convert my database data to csv file(import data to csv file using csv package) now i want to again use this file in my application and store csv file data to my sqlite database how can i convert csv file to json? cause sqlite work with json or i should use from other approach please guide me.
static Future conertToCSVFile(List<Roznamcha> roznamcha) async {
List<List<dynamic>> data = [
...roznamcha.map((e) => [
e.transactionNumber,
e.issueDate,
e.description,
e.credit,
e.debit,
e.rate,
e.accountNum,
])
];
Directory dir = Platform.isAndroid
? await getExternalStorageDirectory() //FOR ANDROID
: await getApplicationSupportDirectory(); //FOR iOS
final paths = '${dir.path}/csv.csv';
String csvData = ListToCsvConverter().convert(data);
final File file = File(paths);
await file.writeAsString(csvData);
}
static loadCsvFromStorage() async {
FilePickerResult result = await FilePicker.platform.pickFiles(
allowedExtensions: ['csv'],
type: FileType.custom,
);
String path = result.files.first.path;
final csvFile = new File(path).openRead();
return await csvFile
.transform(utf8.decoder)
.transform(
CsvToListConverter(),
)
.toList();
}
}
Copyright Notice:Content Author:「maliha arash」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67859748/how-to-convert-csv-file-to-json-in-flutter