I need the index number forEach iterable value because I want to update my data table. But for updating the data table I need the column ID. That's why I want the index number for each value and assign it to > i. There are similar questions which was explained how to get the index of iterable. But in my case, I am failed to map the CallLogEntry and that's why I can not able to get the index of this iterable value.
Future callLogDB() async {
Iterable<CallLogEntry> cLog = await CallLog.get();
final dbHelper = DatabaseHelper.instance;
int i = 0;
cLog.forEach((log) async {
i++;
// row to insert
Map<String, dynamic> row = {
//DatabaseHelper.columnId: 2,
DatabaseHelper.columnName: '${log.name}',
DatabaseHelper.columnNumber: '${log.number}',
DatabaseHelper.columnType: '${log.callType}',
DatabaseHelper.columnDate:
'${DateTime.fromMillisecondsSinceEpoch(log.timestamp)}',
DatabaseHelper.columnDuration: '${log.duration}'
};
await dbHelper.insert(row);
print('CallLog $i:: $row');
});
return cLog;
}
I can not map CallLogEntry.
First, you need to convert Iterable<CallLogEntry>
into a list using toList()
method then you can use asMap()
SAMPLE CODE
Iterable<CallLogEntry> cLog = await CallLog.get();
cLog.toList().asMap().forEach((cLogIndex, callLogEntry) {
row.forEach((index, data) {
});
});
EDIT
Future callLogDB() async {
Iterable<CallLogEntry> cLog = await CallLog.get();
final dbHelper = DatabaseHelper.instance;
int i = 0;
cLog.toList().asMap().forEach((cLogIndex, callLogEntry) {
Map<String, dynamic> row = {
//DatabaseHelper.columnId: cLogIndex,
DatabaseHelper.columnName: '${cLogIndex.name}',
DatabaseHelper.columnNumber: '${cLogIndex.number}',
DatabaseHelper.columnType: '${cLogIndex.callType}',
DatabaseHelper.columnDate:
'${DateTime.fromMillisecondsSinceEpoch(cLogIndex.timestamp)}',
DatabaseHelper.columnDuration: '${cLogIndex.duration}'
};
await dbHelper.insert(row);
});
return cLog;
}