fluttercommunity / flutter_contacts

Contacts Service - A Flutter plugin to retrieve and manage contacts on Android and iOS devices. Maintainer: @lukasgit
https://pub.dev/packages/contacts_service
MIT License
153 stars 69 forks source link

Cant Add Contacts #31

Closed deidyomega closed 5 years ago

deidyomega commented 6 years ago

Your add contact example is one line, with no guide on how to initialize the contact.

Looking at the source, I think my below code should work, but it fails with below error. Any idea on how I'm doing wrong?

(Tried on Android 9 && Android 8)

Code:

import 'package:contacts_service/contacts_service.dart';
import 'dart:async';

Future<void> generateContact(String name, String phone) async {
    Item phoneNumber = Item.fromMap({
      "label": "mobile",
      "phone": phone
    });
    await ContactsService.addContact(Contact(phones: [phoneNumber], givenName: name, familyName: "SOME DATA"));
}

Error:

E/flutter (23087): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (23087): FormatException: Invalid envelope
E/flutter (23087): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:549:7)
E/flutter (23087): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:279:18)
E/flutter (23087): <asynchronous suspension>
E/flutter (23087): #2      ContactsService.addContact (package:contacts_service/contacts_service.dart:18:16)
E/flutter (23087): #3      generateContact (package:annuitycheck/api/address.dart:9:27)
E/flutter (23087): <asynchronous suspension>
E/flutter (23087): #4      HomePageState.generateCard.<anonymous closure> (package:annuitycheck/pages/home.dart:45:21)
E/flutter (23087): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14)
E/flutter (23087): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:549:30)
E/flutter (23087): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (23087): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter (23087): #9      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123:7)
E/flutter (23087): #10     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (23087): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
E/flutter (23087): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter (23087): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (23087): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter (23087): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter (23087): #16     _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter (23087): #17     _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
clovisnicolas commented 6 years ago

Some people seem to have this invalid enveloppe issue which i'm unable to reproduce on my end... Could you run the example and see if you have the same issue ? If so could you check where exactly it crashes ?

Thanks

deidyomega commented 6 years ago

screenshot_1536013085

I can't get your example to run at all. However my example -->

Create a new flutter project, copy/paste my function in, and have it execute on the example btn click, and you'll see the crash. I tried creating the min required for the bug to appear.

Built build\app\outputs\apk\app-debug.apk.
E/AndroidRuntime(21329): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(21329): Process: flutter.plugins.contactsservice.contactsserviceexample, PID: 21329
E/AndroidRuntime(21329): java.lang.RuntimeException: An error occurred while executing doInBackground()
E/AndroidRuntime(21329):    at android.os.AsyncTask$3.done(AsyncTask.java:353)
E/AndroidRuntime(21329):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
E/AndroidRuntime(21329):    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
E/AndroidRuntime(21329):    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
E/AndroidRuntime(21329):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
E/AndroidRuntime(21329):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
E/AndroidRuntime(21329):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
E/AndroidRuntime(21329):    at java.lang.Thread.run(Thread.java:764)
E/AndroidRuntime(21329): Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{84630f3 21329:flutter.plugins.contactsservice.contactsserviceexample/u0a81} (pid=21329, uid=10081) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
E/AndroidRuntime(21329):    at android.os.Parcel.readException(Parcel.java:2004)
E/AndroidRuntime(21329):    at android.os.Parcel.readException(Parcel.java:1950)
E/AndroidRuntime(21329):    at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4758)
E/AndroidRuntime(21329):    at android.app.ActivityThread.acquireProvider(ActivityThread.java:5836)
E/AndroidRuntime(21329):    at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2526)
E/AndroidRuntime(21329):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1780)
E/AndroidRuntime(21329):    at android.content.ContentResolver.query(ContentResolver.java:738)
E/AndroidRuntime(21329):    at android.content.ContentResolver.query(ContentResolver.java:704)
E/AndroidRuntime(21329):    at android.content.ContentResolver.query(ContentResolver.java:662)
E/AndroidRuntime(21329):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin.getCursor(ContactsServicePlugin.java:145)
E/AndroidRuntime(21329):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin.access$100(ContactsServicePlugin.java:34)
E/AndroidRuntime(21329):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask.doInBackground(ContactsServicePlugin.java:120)
E/AndroidRuntime(21329):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask.doInBackground(ContactsServicePlugin.java:115)
E/AndroidRuntime(21329):    at android.os.AsyncTask$2.call(AsyncTask.java:333)
E/AndroidRuntime(21329):    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/AndroidRuntime(21329):    ... 4 more
clovisnicolas commented 6 years ago

@deidyomega I see "Permission Denial", have you checked if permissions were enabled?

deidyomega commented 6 years ago

I did check. I literally cloned your repo, cd'd into the directory and ran the app. I checked, your repo's permissions look correct.

Hey, if you'd be willing to join me on a gotomeeting / skype or something, and resolve this, I'd give you 40 bucks. I know it's not a lot, but it might help you see what is the underlying issue.

lukasgit commented 6 years ago

@deidyomega @clovisnicolas when you've figured out the problem can you post the solution for the rest of us to understand what was done to resolve the issue?

clovisnicolas commented 6 years ago

Can you try this plugin https://github.com/Ethras/flutter_simple_permissions And ask for permissions before adding/fetching contacts ?

deidyomega commented 6 years ago

@lukasgit I had my team abandon flutter as not having a working contact system is blocking, it really needs to be part of core. I'll look at flutter next year and see if they've gotten basic features like contacts working.

So to answer your question, there is no solution. My 9 lines breaks when adding to the default sample app. And there are no alternative libraries that allow you to add contacts.

clovisnicolas commented 6 years ago

@deidyomega I really do believe you are missing Contact permissions on your app.

The error you showed here is exactly the one I have when freshly installing the app on my device, before allowing the app to access contacts.

This plugin does not handle permissions, either the user has to set them manually or use a 3rd party plugin such as https://github.com/Ethras/flutter_simple_permissions

bfproject commented 5 years ago

As @clovisnicolas mention, you should handle Permission.WriteContacts permission before adding a new contact, using flutter_simple_permissions plugin:

bool isPermissionGranted = await SimplePermissions.checkPermission(Permission.WriteContacts);
lukasgit commented 5 years ago

See @clovisnicolas and @bfproject comments above. Closing this issue.