Search code examples
flutterdartpackagecallcalllog

Simply accessing call logs in flutter (?)


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,
    );

Solution

  • 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.