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
- Execute
final account = await FlutterContacts.accounts.getDefault(); (Returns null on affected devices).
- Create a group:
final group = await FlutterContacts.groups.create('Test Group', account: account);
- Create a contact
final contactId = await FlutterContacts.create(Contact(name: Name(first: 'John')));
- 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();
}
Description
On certain Android devices (e.g., LG devices running LG UX/Android 9/10), calling
FlutterContacts.groups.addContacts()throws aPlatformExceptionwith the message:Failed to handle addContacts: Group has no account: <groupId>.This occurs because
FlutterContacts.accounts.getDefault()returnsnullon these devices. Consequently, passingaccount: nulltoFlutterContacts.groups.create()creates a group in the Android Contacts database withoutACCOUNT_NAMEandACCOUNT_TYPE. WhenaddContacts()is subsequently called on that group ID, the native Android plugin implementation fails because the group is not associated with any account.Environment
flutter_contactsSteps to Reproduce
final account = await FlutterContacts.accounts.getDefault();(Returnsnullon affected devices).final group = await FlutterContacts.groups.create('Test Group', account: account);
final contactId = await FlutterContacts.create(Contact(name: Name(first: 'John')));
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();
}