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

Can't read contacts #61

Closed Calin-Cosma closed 5 years ago

Calin-Cosma commented 5 years ago

I am using an emulation of a Pixel 2 (Android 28).

pubspec.yaml

environment:
    sdk: ">=2.0.0-dev.68.0 <3.0.0"

dependencies:
    flutter:
        sdk: flutter

    # The following adds the Cupertino Icons font to your application.
    # Use with the CupertinoIcons class for iOS style icons.
    cupertino_icons: ^0.1.2
    path_provider: '>=0.3.0'
    sqflite: any
    uuid: 2.0.0
    quiver: '>=2.0.0 <3.0.0'
    flutter_speed_dial: ^1.1.1
    contacts_service: '>=0.1.1'
    simple_permissions: 0.1.7

After adding the dependencies to contacts_service and simple_permissions and some test code, I get:

Syncing files to device Android SDK built for x86...
I/flutter (20912): Found 0 Contacts
Reloaded 2 of 492 libraries in 4,785ms.
E/flutter (20912): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter (20912): MissingPluginException(No implementation found for method requestPermission on channel simple_permissions)
E/flutter (20912): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:291:7)
E/flutter (20912): <asynchronous suspension>
E/flutter (20912): #1      SimplePermissions.requestPermission (package:simple_permissions/simple_permissions.dart:24:35)
E/flutter (20912): <asynchronous suspension>
E/flutter (20912): #2      ContactsScreenState._addContact (package:my_app/contact_list.dart:118:27)
E/flutter (20912): <asynchronous suspension>
E/flutter (20912): #3      ContactsScreenState.build.<anonymous closure> (package:my_app/contact_list.dart:85:20)
E/flutter (20912): #4      AnimatedChild._performAction (package:flutter_speed_dial/src/animated_child.dart:60:24)
E/flutter (20912): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
E/flutter (20912): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
E/flutter (20912): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (20912): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (20912): #9      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
E/flutter (20912): #10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter (20912): #11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (20912): #12     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (20912): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:180:19)
E/flutter (20912): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
E/flutter (20912): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
E/flutter (20912): #16     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (20912): #17     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
E/flutter (20912): #18     _invoke1 (dart:ui/hooks.dart:168:13)
E/flutter (20912): #19     _dispatchPointerDataPacket (dart:ui/hooks.dart:122:5)
Lost connection to device.

If I try to do

flutter clean

and then try to run the app, I get:

Launching lib/main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:preDebugBuild'.
> Android dependency 'com.android.support:support-core-utils' has different version for the compile (27.1.1) and runtime (28.0.0-rc01) classpath. You should manually set the same version via DependencyResolution

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 6s
Finished with error: Gradle task assembleDebug failed with exit code 1

The code of my function:

    _addContact(BuildContext context) async {
        print("_addContact called");

        await SimplePermissions.requestPermission(Permission.ReadContacts);
        await SimplePermissions.requestPermission(Permission.WriteContacts);

        bool isReadPermissionGranted = await SimplePermissions.checkPermission(Permission.ReadContacts);
        bool isWritePermissionGranted = await SimplePermissions.checkPermission(Permission.WriteContacts);
        if (isReadPermissionGranted && isWritePermissionGranted) {
            Iterable<Contact> contacts = await ContactsService.getContacts();
            print('Found ${contacts.length} contacts');
        } else {
            print('Permissions not granted');
        }
    }

I did add the required lines in the Android Manifest and plist, and in fact it works in the iOs emulator.

Calin-Cosma commented 5 years ago

Can you try using a different package to handle permissions? The simple_permissions repository seems to have either been deleted or made private.

I actually tried these 2:

https://github.com/AppleEducate/flutter_simple_permissions ( https://pub.dartlang.org/packages/contacts_service links to it)

and

https://pub.dartlang.org/packages/permission_handler

The first one got me the error I posted. The second one cause a whole different error (I posted an issue on their Github, too).

I am quite new to Flutter/Dart, and while it seems like a pretty cool language with a thriving community, it's disappointing that some basic stuff is missing (like being able to access the contacts) and has to be developed by the community.

Since I am new to Flutter, I am starting to wonder if I am wasting my time trying to build an app with it. If accessing the contacts is so difficult, how many of these problems will I run into, even for a relatively simple app ?

It would be great if the devs for contacts_service could update their README and put some working code there.

trinqk commented 5 years ago

@Calin-Cosma The issue you are facing may be because of this Flutter update. It broke many plugins for Android(which is probably why it was working on your iOS simulator), and flutter_contacts was one of them.

flutter_contacts was migrated to AndroidX quite recently, so it should be working now. Try using the latest version of flutter_contacts contacts_service: ^0.2.1, and migrate to AndroidX.

lukasgit commented 5 years ago

@Calin-Cosma bug fixes have been resolved. Please use the latest version 0.2.3 and review the example app for ways to implement into your app.

I've tested extensively Android Pixel 2 (API 28) and iOS. Everything is working. Closing this issue, but feel free to re-open if you think there's a problem with the plugin.