skip to Main Content

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?

enter image description here

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


  1. By your log, you can notice that all of the properties regarding your contact are missing:

    I/flutter ( 9205): [Contact(id=253, displayName=A. H pahlozadeh, thumbnail=null, photo=null, isStarred=true, name=Name(first=, last=, middle=, prefix=, suffix=, nickname=, firstPhonetic=, lastPhonetic=, middlePhonetic=), phones=[], emails=[], addresses=[], organizations=[], websites=[], socialMedias=[], events=[], notes=[], accounts=[], groups=[])
    

    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:

    Future _fetchContacts() async {
        if (!await FlutterContacts.requestPermission()) {
        } else {
          List<Contact> contacts = await FlutterContacts.getContacts(
      withProperties: true); /// <---- HERE
          setState(() => _contacts = contacts);
          print(_contacts);
        }
        return _contacts;
      }
    

    You can also see this in the example of the package in pub.dev.

    Login or Signup to reply.
  2. if you change your code like this everythings will be ok

    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<void> _fetchContacts() async {
        if (!await FlutterContacts.requestPermission()) {
        } else {
          List<Contact> contacts = await FlutterContacts.getContacts(withProperties: true);
          setState(() { _contacts = contacts; print(_contacts);});
        }
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: ListView.builder(
                itemCount: _contacts.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(_contacts[index].groups[0].name),
                  );
                })
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search