17074

Fetching contacts in flutter

<h3>Question</h3>

I am trying to get contacts from device device using 'contacts_service' plugin. It is working fine on my device but not working any other device.

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

This is the error message I am getting

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
<h3>Answer1:</h3>

May be other devices don't have permission for reading the contacts. Using flutter_simple_permissions plugin you can ask for permission.

So a basic and valid initialState method could look like this:

@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(); }
<h3>Answer2:</h3>

Add Permission to ManifestFiles (there are two Manifest.xml in flutter Project)

main/AndroidManifest.xml

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

use Package : contacts_service 0.3.3

https://pub.dev/packages/contacts_service

来源:https://stackoverflow.com/questions/57921348/fetching-contacts-in-flutter

Recommend

  • Why is this BindingAdapter not working in Kotlin?
  • How to access Google Search “I'm Feeling Lucky” functionality using API?
  • Hibernate @Formula doesn't include Schema
  • How do I put booleans into a mysql database with prepared statements?
  • reset is not a function in JavaScript [duplicate]
  • set value in combobox inside datagridview
  • How to fix the beginner's ReactJS error?
  • Save cropped image from ng-src
  • iframe cross-domain access
  • .NET COM assembly interacting with Excel via BackgroundWorker
  • How to create a timeline chart control thing?
  • Starting activity from phonegap plugin-webIntent
  • How to add a CAGradientLayer to a UIView when using programmatic constraints
  • Rebuilding again and again to view the imported QML file changes
  • Rails Template Error with Heroku
  • How to get a Builder object from rows related to pivot - Laravel
  • Cycle R,G,B vales as HUE?
  • Get an image from the video
  • How to make responsive images in flutter?
  • using vtkTimerCallback with QVTKRenderWindowInteractor not working
  • ggplot2 facet_grid with distinct x-axis labels using facet_grid
  • Hyperlink to Outlook Attachment
  • What's the difference between using RDFS/OWL and XML?
  • SELECT on JSONField with Django
  • watir webdriver - window not found
  • Adding native code to an existing Worklight hybrid app
  • Circular Left Rotation Algorithm in C#
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Add font awesome icon to custom add to cart button in Woocommerce 3
  • how to read to huge file into buffer
  • How to handle div that is created dynamically in a table
  • Google App Engine backend servlet not responding