I\'m using contact_picker in my flutter app to access fetch a contact from the phone\'s contacts. When I click on the contact name in the phonebook, the app crashes.
Th
You need to edit the following file C:\your pub-cache directory\contact_picker-0.0.1+2\android\src\main\java\net\goderbauer\flutter\contactpicker\ContactPickerPlugin.java and replace with the following code.
// Copyright 2017 Michael Goderbauer. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package net.goderbauer.flutter.contactpicker;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import java.util.HashMap;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import static android.app.Activity.RESULT_OK;
public class ContactPickerPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener {
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "contact_picker");
ContactPickerPlugin instance = new ContactPickerPlugin(registrar.activity());
registrar.addActivityResultListener(instance);
channel.setMethodCallHandler(instance);
}
private ContactPickerPlugin(Activity activity) {
this.activity = activity;
}
private static int PICK_CONTACT = 2015;
private Activity activity;
private Result pendingResult = null;
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("selectContact")) {
if (pendingResult != null) {
pendingResult.error("multiple_requests", "Cancelled by a second request.", null);
pendingResult = null;
}
pendingResult = result;
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
activity.startActivityForResult(i, PICK_CONTACT);
} else {
//result.notImplemented();
pendingResult = null;
}
}
@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PICK_CONTACT) {
return false;
}
if (resultCode != RESULT_OK) {
pendingResult.success(null);
pendingResult = null;
}
if(pendingResult != null){
Uri contactUri = data.getData();
Cursor cursor = activity.getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
int phoneType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
String label = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(activity.getResources(), phoneType, customLabel);
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String fullName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
HashMap phoneNumber = new HashMap();
phoneNumber.put("number", number);
phoneNumber.put("label", label);
HashMap contact = new HashMap();
contact.put("fullName", fullName);
contact.put("phoneNumber", phoneNumber);
pendingResult.success(contact);
pendingResult = null;
return true;
}
else{
pendingResult = null;
return false;
}
}
}
Cheers!