6

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;
  }
Santo Shakil
  • 981
  • 2
  • 13
  • 27

1 Answers1

12

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;
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • You helped me previously in a very easy way. I tried your solution of this post. But I'm failed. Maybe I can't implement your solution properly. Can please edit my code with your solution. Please. – Santo Shakil Jun 23 '20 at 05:42
  • You are awesome !!! It worked with slight modification. cLogIndex.nember give error because cLogIndex is the index number. I tried callLogEntry.number. And it Works!!! Thank you so much. – Santo Shakil Jun 23 '20 at 05:57
  • Please see these answers https://stackoverflow.com/a/59447850/56974 and https://stackoverflow.com/a/68357576/56974 – Michael Ekoka Jul 09 '22 at 05:58