QuisApp / flutter_contacts

MIT License
88 stars 143 forks source link

PlatformException on contact update #95

Open yassinsameh opened 1 year ago

yassinsameh commented 1 year ago

Generally the update method is working correctly for most users, when viewing crashlytics logs however i find this error coming up for more than one user.

Unable to have any more information, could you please provide any insight on how to get more information or the cause of this? @joachimvalente @joachim-quis

Error: PlatformException(unknown error, unknown error, The operation couldn’t be completed. (CNErrorDomain error 2.), null). Error thrown null.

Stacktrace:

0  ???                            0x0 StandardMethodCodec.decodeEnvelope + 653 (message_codecs.dart:653)
1  ???                            0x0 MethodChannel._invokeMethod + 296 (platform_channel.dart:296)
2  ???                            0x0 FlutterContacts.updateContact + 189 (flutter_contacts.dart:189)

flutter_contacts: 1.1.5+1

yassinsameh commented 1 year ago

Another form of the error: PlatformException(unknown error, unknown error, Violated Constraints Error, null). Error thrown null.

yassinsameh commented 1 year ago

The only similar issue i could find is here: https://stackoverflow.com/questions/33033163/cncontactstore-executesaverequest-failing-with-cnerrordomain-error-2

It recommends not setting the fields as empty array unless the contact has objects for that certain attribute.

So to follow up on that, I would have 2 questions:

1) In the package why is it that in the addFieldsToContact method, all args such as args["emails"] is assumed to never be null. 2) Does it make sense to change all variables to the following syntax (Checks for null and only sets item to empty array first if it doesn't exist in args, and that would result in removing the clearFieldsMethod which sets to empty array in all cases)

This:

if let websites = args["websites"] as? [[String: Any]], !websites.isEmpty {
            contact.urlAddresses = []
            websites.forEach {
                Website(fromMap: $0).addTo(contact)
            }
        }

Instead of this:

(args["websites"] as! [[String: Any]]).forEach {
            Website(fromMap: $0).addTo(contact)
        }

@scroollocker @anggrayudi @jadasi @joachimvalente

joachim-quis commented 1 year ago

Hi @yassinsameh, thanks for reporting and looking into this. This suggestion makes a lot of sense. Did it fix your issue? If so, I'll add it to the next version.

yassinsameh commented 1 year ago

Hi @joachim-quis , after the changes outlined i no longer get the "CNErrorDomain error 2" , I still do get the "Violated constraints error" Which i believe is caused by Read-Only contacts attempting to be updated, is there a way we can add that boolean field in the Flutter Contact so developers can be aware of this to not attempt updating?

yassinsameh commented 1 year ago

@joachim-quis following up on this, any idea how we can detect if it is read only and add that boolean to the Contact object?