Get Phone Contacts

In the current age, Contacts is very useful in mobile app for sharing. Here I try to implement Get phone contacts in Flutter App.

You can Download Source Code via Github.

Form here we start to implement, First we add lib file in .yaml file

Add the following libs to your pubspec.yaml file:

permission_handler: ^5.0.0
contacts_service: ^0.3.10
url_launcher: ^5.4.2

First we check permission for Read Phone Contacts

final PermissionStatus permissionStatus = await _getPermission();
        if (permissionStatus == PermissionStatus.granted) {
          Navigator.push(
              context, MaterialPageRoute(builder: (context) => ContactsPage()));
        } else {
          //If permissions have been denied show standard cupertino alert dialog
          showDialog(
              context: context,
              builder: (BuildContext context) => CupertinoAlertDialog(
                    title: Text('Permissions error'),
                    content: Text('Please enable contacts access '
                        'permission in system settings'),
                    actions: <Widget>[
                      CupertinoDialogAction(
                        child: Text('OK'),
                        onPressed: () => Navigator.of(context).pop(),
                      )
                    ],
                  ));
        }

Get Phone Contacts

Future<void> getContacts() async {
    //We already have permissions for contact when we get to this page, so we
    // are now just retrieving it
    final Iterable<Contact> contacts = await ContactsService.getContacts();
    setState(() {
      _contacts = contacts;
    });
  }

contactsPage.dart

import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';

class ContactsPage extends StatefulWidget {
  @override
  _ContactsPageState createState() => _ContactsPageState();
}

class _ContactsPageState extends State<ContactsPage> {
  Iterable<Contact> _contacts;

  @override
  void initState() {
    getContacts();
    super.initState();
  }

  Future<void> getContacts() async {
    //We already have permissions for contact when we get to this page, so we
    // are now just retrieving it
    final Iterable<Contact> contacts = await ContactsService.getContacts();
    setState(() {
      _contacts = contacts;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: (Text('Contacts')),
      ),
      body: _contacts != null
          //Build a list view of all contacts, displaying their avatar and
          // display name
          ? ListView.builder(
              itemCount: _contacts?.length ?? 0,
              itemBuilder: (BuildContext context, int index) {
                Contact contact = _contacts?.elementAt(index);
                return ListTile(
                  contentPadding:
                      const EdgeInsets.symmetric(vertical: 2, horizontal: 18),
                  leading: (contact.avatar != null && contact.avatar.isNotEmpty)
                      ? CircleAvatar(
                          backgroundImage: MemoryImage(contact.avatar),
                        )
                      : CircleAvatar(
                          child: Text(contact.initials()),
                          backgroundColor: Theme.of(context).accentColor,
                        ),
                  title: Text(contact.displayName ?? ''),
                );
              },
            )
          : Center(child: const CircularProgressIndicator()),
    );
  }
}

You can Download Source Code via Github.

7 Replies to “Get Phone Contacts”

  1. It is in reality a great and useful piece of information. I’m
    glad that you shared this useful info with us. Please stay us up to date
    like this. Thanks for sharing.

  2. It is truly a nice and useful piece of information. I’m satisfied that
    you simply shared this helpful information with us. Please stay us up
    to date like this. Thank you for sharing.

  3. Hi!

    I am an expert in social media marketing and I would like to know if you are looking for help with generating sales through social media.

    Let me know if this is something you’re interested in and I can provide you with more information.

    Mathew Barrett

Leave a Reply

Your email address will not be published.