颤振中获取触点

时间:2019-09-13 09:54:40

标签: flutter dart

我正在尝试使用“ contacts_service”插件从设备设备获取联系人。在我的设备上运行正常,但在其他任何设备上均无法运行。

    await PermissionHandler().requestPermissions([
      PermissionGroup.contacts,
    ]);
    try {
      var x = await ContactsService.getContacts();
      print(x);
    } catch (e) {
      print(e);
    }

这是我收到的错误消息

FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(17213): Process: com.chat.amrirez_chat, PID: 17213
E/AndroidRuntime(17213): java.lang.RuntimeException: An error occurred while executing doInBackground()
E/AndroidRuntime(17213):    at android.os.AsyncTask$3.done(AsyncTask.java:318)
E/AndroidRuntime(17213):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
E/AndroidRuntime(17213):    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
E/AndroidRuntime(17213):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(17213):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
E/AndroidRuntime(17213):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
E/AndroidRuntime(17213):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
E/AndroidRuntime(17213):    at java.lang.Thread.run(Thread.java:761)
E/AndroidRuntime(17213): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
E/AndroidRuntime(17213):    at flutter.plugins.contactsservice.contactsservice.Contact.compareTo(Contact.java:94)
E/AndroidRuntime(17213):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask$1.compare(ContactsServicePlugin.java:175)
E/AndroidRuntime(17213):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask$1.compare(ContactsServicePlugin.java:172)
E/AndroidRuntime(17213):    at java.util.TimSort.binarySort(TimSort.java:292)
E/AndroidRuntime(17213):    at java.util.TimSort.sort(TimSort.java:235)
E/AndroidRuntime(17213):    at java.util.Arrays.sort(Arrays.java:1523)
E/AndroidRuntime(17213):    at java.util.Collections.sort(Collections.java:238)
E/AndroidRuntime(17213):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask.doInBackground(ContactsServicePlugin.java:178)
E/AndroidRuntime(17213):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask.doInBackground(ContactsServicePlugin.java:137)
E/AndroidRuntime(17213):    at android.os.AsyncTask$2.call(AsyncTask.java:304)
E/AndroidRuntime(17213):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(17213):    ... 4 more

2 个答案:

答案 0 :(得分:0)

可能是其他设备无权读取联系人。使用flutter_simple_permissions插件,您可以请求权限。

因此基本有效的initialState方法可能如下所示:

  @override
  void initState() {
    SimplePermissions
      .requestPermission(Permission.ReadContacts)
      .then((status) {
        if (status != PermissionStatus.authorized) {
          debugPrint("no read rights. Aborting mission!");
          return;
        }
        debugPrint("start loading contacts");
        ContactsService.getContacts().then((foundContacts) {
          debugPrint("done loading contacts" + foundContacts.length.toString());
          final list = foundContacts.toList();
          list.sort((a, b) => a.givenName.compareTo(b.givenName));
          setState(() {
            contacts = list;
          });
        }).catchError((error) {
          debugPrint(error.toString());
        });
      });
    super.initState();
  }

答案 1 :(得分:0)

向ManifestFiles添加权限(在Flutter Project中有两个Manifest.xml)

main / AndroidManifest.xml

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

使用软件包: contacts_service 0.3.3

https://pub.dev/packages/contacts_service