flutter access call logs

Viewed 1827

i want to get my call logs and i used call logs package and on button click, i call tthis function

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

but its shooting me an error like this

[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)

PS: i am using this package: call_log: ^3.0.3

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

Add this to your android/app/src/main/AndroidManifest.xml file just under the first < manifest> tag.

At first you need to add permission to manifest so operating system will know that your app can ask for log reading. for that, you need to go to:

android/app/src/main/AndroidManifest.xml


android/app/src/debug/AndroidManifest.xml

You need to paste this permission under manifest<>

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

So, now you have to ask user to grant this request. for that you can use this plugin permission_handler

You can ask for permission like this:

void getPermissionUser() async {
    if (await Permission.phone.request().isGranted) {
      getLog();
    } else {
      await Permission.phone.request();
    }
  }

Now you can read call logs without any errors.

If the permission to access call logs was denied, this error would occur

This package is not supported anymore and not working for me. after searching along I found this package from the flutter plugin and I made my own fork and migrated it to null safety and it's now working check it out as a working alternative

Related