How to get access to your contacts in Flutter

Here, you’ll learn how t access your device contacts and get necessary in formation.

First, import the contacts_service package from pub.

Also import permission_handler to provide necessary permissions.

Create a new dart file and paste this code:

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

 class AccessContacts extends StatefulWidget {
   @override
   _AccessContactsState createState() => _AccessContactsState();
 }

 class _AccessContactsState extends State<AccessContacts> {
   Iterable<Contact> _contacts;

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

   getContacts() async {
     PermissionStatus permissionStatus = await _getPermission();
     if (permissionStatus == PermissionStatus.granted) {
       var contacts = await ContactsService.getContacts();
       setState(() {
         _contacts = contacts;
       });
     } else {
       throw PlatformException(
         code: 'PERMISSION_DENIED',
         message: 'Access to location data denied',
         details: null,
       );
     }
   }

   Future<PermissionStatus> _getPermission() async {
     PermissionStatus permission = await PermissionHandler()
         .checkPermissionStatus(PermissionGroup.contacts);
     if (permission != PermissionStatus.granted &&
         permission != PermissionStatus.disabled) {
       Map<PermissionGroup, PermissionStatus> permisionStatus =
           await PermissionHandler()
               .requestPermissions([PermissionGroup.contacts]);
       return permisionStatus[PermissionGroup.contacts] ??
           PermissionStatus.unknown;
     } else {
       return permission;
     }
   }

   @override
   Widget build(BuildContext context) {
     return Scaffold(
       appBar: AppBar(title: Text('Access Contacts example')),
       body: _contacts != null
           ? ListView.builder(
               itemCount: _contacts?.length ?? 0,
               itemBuilder: (context, index) {
                 Contact c = _contacts?.elementAt(index);
                 return ListTile(
                   leading: (c.avatar != null && c.avatar.length > 0)
                       ? CircleAvatar(
                           backgroundImage: MemoryImage(c.avatar),
                         )
                       : CircleAvatar(child: Text(c.initials())),
                   title: Text(c.displayName ?? ''),
                 );
               },
             )
           : CircularProgressIndicator(),
     );
   }
 }

In main.dart:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch: Colors.red),
      home: new AccessContacts(),
    );
  }
}

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.