edgaras
05/21/2022, 12:13 PMNeedle
05/21/2022, 12:13 PMNeedle
05/21/2022, 12:13 PMMuezz
05/21/2022, 12:25 PMNeedle
05/21/2022, 12:25 PMlanbau
05/21/2022, 2:00 PMedgaras
05/21/2022, 2:05 PMMuezz
05/21/2022, 4:48 PMedgaras
05/22/2022, 6:50 AMMuezz
05/22/2022, 7:01 AMdart
Future<Result> addTransaction(TransactionModel transaction) async {
var response = await _transactionsDB.insert(transaction.toMap()).execute();
if (response.error != null) {
return Result.error(message: response.error!.message);
} else {
return Result.success(message: 'Success');
}
}
This is my data class's method that converts the class instance to a map (a map and json is the same thing in dart)
dart
Map<String, dynamic> toMap() {
return {
't_date': tDate!.toIso8601String(),
'category': category,
'amount': amount,
'deb_acc': debAcc,
'cred_acc': credAcc,
'item_name': itemName,
'prod_price': prodPrice,
'prod_quantity': prodQuantity,
};
}
Keep in mind that the "keys" in this json/map have the be the exact and correct names of your table's columns. If one is incorrect, that value would not be added.
As you can see, I am converting my data class instance to a map, you have to figure out a way to convert a CSV to a map/json. I dont think that would be too hard.
As for actually inserting the data in bulk, I dont think that the insert
method supports multiple insertions in one call. I may be wrong but if that is the case, you can have a for
loop for each line of the CSV converted to json and add that.