Skip to content

[Android] PlatformException: Failed to handle addContacts: Group has no account: <groupId> when getDefault() returns null #247

Description

@Eunno-An

Description

On certain Android devices (e.g., LG devices running LG UX/Android 9/10), calling FlutterContacts.groups.addContacts() throws a PlatformException with the message:
Failed to handle addContacts: Group has no account: <groupId>.

This occurs because FlutterContacts.accounts.getDefault() returns null on these devices. Consequently, passing account: null to FlutterContacts.groups.create() creates a group in the Android Contacts database without ACCOUNT_NAME and ACCOUNT_TYPE. When addContacts() is subsequently called on that group ID, the native Android plugin implementation fails because the group is not associated with any account.

Environment

  • Device: LG Q9 (LG UX, Android 9 / 10)
  • Plugin: flutter_contacts
  • Platform: Android

Steps to Reproduce

  1. Execute final account = await FlutterContacts.accounts.getDefault(); (Returns null on affected devices).
  2. Create a group:
    final group = await FlutterContacts.groups.create('Test Group', account: account);
  3. Create a contact
    final contactId = await FlutterContacts.create(Contact(name: Name(first: 'John')));
  4. Attempt to add the contact to the created group
    await FlutterContacts.groups.addContacts(
    groupId: group.id,
    contactIds: [contactId],
    );

Actual Behavior

I/flutter: PlatformException(flutter_contacts_error, Failed to handle addContacts: Group has no account: 17, null, null)

Expected Behavior

Either FlutterContacts.accounts.getDefault() should fallback to the first available account from FlutterContacts.accounts.getAccounts() if default account resolution returns null.
Or FlutterContacts.groups.create() / addContacts() should handle account: null gracefully without failing when adding contacts to groups on Android.

Proposed Solution / Workaround

Developers currently need to manually fallback to existing accounts when getDefault() returns null:

final accounts = await FlutterContacts.accounts.getAccounts();
Account? account;
if (accounts.isNotEmpty) {
account = accounts.firstWhere(
(a) => a.type == 'com.google',
orElse: () => accounts.first,
);
} else {
account = await FlutterContacts.accounts.getDefault();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions