Simply accessing call logs in flutter (?)

3,664

What I could understand from your question is, you are unable to work with the output of CallLog.get() here.

After adding the package to pubspec.yaml file dependencies and importing it, you can call the get() function using the following line of code -

Iterable<CallLogEntry> entries = await CallLog.get();

It returns an Iterable of type CallLogEntry. An iterable is simply a collection of values, or "elements", that can be accessed sequentially.

The output gets stored in entries which can then be iterated over to access the values such as -

  void _callLogs() async {
  Iterable<CallLogEntry> entries = await CallLog.get();
  for (var item in entries) {
    print(item.name);
  }
}

The above code snippet would print the names of all CallLog entries. Try replacing item.name with item.number, item.duration, item.callType.

Also, do not forget to add the following line to AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CALL_LOG" />

Instead of CallLog.get(), you can also use CallLog.query() to specify constraints on the response/output as mentioned in the question itself.

Share:
3,664
kamranbekirovyz
Author by

kamranbekirovyz

Updated on December 17, 2022

Comments

  • kamranbekirovyz
    kamranbekirovyz over 1 year

    I need to access the call log of android mobile phone in my project and I don't have that much experience in Flutter. Found a package named 'call_log' but don't know how to implement. I would very much appreciate any help here.

    Here is the readme file of 'call_log' package:

    // IMPORT PACKAGE
    import 'package:call_log/call_log.dart';
    
    // GET WHOLE CALL LOG
    Iterable<CallLogEntry> entries = await CallLog.get();
    
    // QUERY CALL LOG (ALL PARAMS ARE OPTIONAL)
    var now = DateTime.now();
    int from = now.subtract(Duration(days: 60)).millisecondsSinceEpoch;
    int to = now.subtract(Duration(days: 30)).millisecondsSinceEpoch;
    Iterable<CallLogEntry> entries = await CallLog.query(
          dateFrom: from,
          dateTo: to,
          durationFrom: 0,
          durationTo: 60,
          name: 'John Doe',
          number: '901700000',
          type: CallType.incoming,
        );
    
  • Riyazat Durrani
    Riyazat Durrani almost 3 years
    getting this error :[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(INTERNAL_ERROR, Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference, null, null)
  • Viral
    Viral over 2 years
    is there any way to modify/delete that call logs from phone using this package or any other flutter package ?
  • Shalabyer
    Shalabyer over 2 years
    @ankitkumar same here, any news?
  • Shalabyer
    Shalabyer over 2 years
    if anyone concerned about @ankitkumar error check this out stackoverflow.com/a/69344487/13444299