I have grouped the contacts, but when I run the print() command in the console, the list of groups is empty.
Why is the group list empty even though this contact is a member of a group?
import 'package:flutter/material.dart';
import 'package:flutter_contacts/contact.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
class ContactsPage extends StatefulWidget {
const ContactsPage({super.key});
@override
State<ContactsPage> createState() => _ContactsPageState();
}
class _ContactsPageState extends State<ContactsPage> {
List<Contact> _contacts =[];
@override
void initState() {
super.initState();
_fetchContacts();
}
Future _fetchContacts() async {
if (!await FlutterContacts.requestPermission()) {
} else {
List<Contact> contacts = await FlutterContacts.getContacts(withProperties: true);
setState(() => _contacts = contacts);
print(_contacts);
}
return _contacts;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: _fetchContacts(),
builder: (BuildContext context, index) {
return ListView.builder(
itemCount: _contacts.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(_contacts[index].groups[0].name),
);
}, );
},),
);
}
}
console result:
I/flutter ( 9205): [Contact(id=253, displayName=A. H pahlozadeh, thumbnail=null, photo=null, isStarred=false, name=Name(first=A., last=pahlozadeh, middle=H, prefix=, suffix=, nickname=, firstPhonetic=, lastPhonetic=, middlePhonetic=), phones=[], emails=[Email([email protected], label=EmailLabel.other, customLabel=, isPrimary=true)], addresses=[], organizations=[], websites=[Website(url=http://www.google.com/profiles/114734360510822226117, label=WebsiteLabel.profile, customLabel=)], socialMedias=[], events=[], notes=[], accounts=[], groups=[]
2
Answers
By your log, you can notice that all of the properties regarding your contact are missing:
That is because when you request contacts through the FlutterContacts API, you didn’t add the argument to get the properties.
To do so, you can do this:
You can also see this in the example of the package in pub.dev.
if you change your code like this everythings will be ok