Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get index number forEach iterable value in Dart (Flutter)

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;
  }
like image 718
Santo Shakil Avatar asked Jun 23 '20 05:06

Santo Shakil


1 Answers

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;
}
like image 180
AskNilesh Avatar answered Oct 05 '22 22:10

AskNilesh