如何在 Dart (Flutter) 中获取每个可迭代值的索引号
How to get index number forEach iterable value in Dart (Flutter)
我需要 index 数字 forEach 可迭代值,因为我想更新我的数据 table。但是为了更新数据 table 我需要列 ID。这就是为什么我想要每个值的索引号并将其分配给> i。
有类似的问题解释了如何获取可迭代的索引。但就我而言,我未能映射 CallLogEntry,这就是为什么我无法获取此可迭代值的索引。
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.
首先,您需要使用toList()
方法将Iterable<CallLogEntry>
转换为列表,然后您可以使用asMap()
示例代码
Iterable<CallLogEntry> cLog = await CallLog.get();
cLog.toList().asMap().forEach((cLogIndex, callLogEntry) {
row.forEach((index, data) {
});
});
编辑
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;
}
我需要 index 数字 forEach 可迭代值,因为我想更新我的数据 table。但是为了更新数据 table 我需要列 ID。这就是为什么我想要每个值的索引号并将其分配给> i。 有类似的问题解释了如何获取可迭代的索引。但就我而言,我未能映射 CallLogEntry,这就是为什么我无法获取此可迭代值的索引。
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.
首先,您需要使用toList()
方法将Iterable<CallLogEntry>
转换为列表,然后您可以使用asMap()
示例代码
Iterable<CallLogEntry> cLog = await CallLog.get();
cLog.toList().asMap().forEach((cLogIndex, callLogEntry) {
row.forEach((index, data) {
});
});
编辑
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;
}