Home

Archives

Bio

Github

Whatsapp

Flutter Extract Data Kontak Dengan Contacts_Service Plugin

1:31 PM

Script ini bisa buat aplikasi seperti Truecaller

Extract contact users dan mengirim ke server dalam bentuk json.

Plugin Contacts_Service dan permission_handler

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

Class untuk extract contact


 class MyContact {
  // name
  String displayName = '',
      givenName = '',
      middleName = '',
      prefix = '',
      suffix = '',
      familyName = '';

  //type
  String accountType = '';

  // Company
  String company = '', jobTitle = '';

  List alamats = List();

  List emails = List();

  List phones = List();
  //String avatar = "";

  MyContact(
      {this.displayName,
      this.givenName,
      this.middleName,
      this.prefix,
      this.suffix,
      this.familyName,
      this.company,
      this.jobTitle,
      this.alamats,
      this.emails,
      this.phones,
      this.accountType});

  Map toJson() {
    return {
      "name": displayName,
      "givenName": givenName,
      "middleName": middleName,
      "prefix": prefix,
      "suffix": suffix,
      "familyName": familyName,
      "company": company,
      "jobTitle": jobTitle,
      "accountType": accountType,
      "alamats": alamats.map((alamat) => alamat.toJson()).toList(),
      "emails": emails.map((email) => email.toJson()).toList(),
      "phones": phones.map((phone) => phone.toJson()).toList(),
      //"avatar": avatar,
    };
  }
}

class Phone {
  String titleku = "";
  String phone = "";

  Phone({this.titleku, this.phone});

  Map toJson() {
    return {
      titleku: phone,
    };
  }
}

class Email {
  String titleku = "";
  String email = "";

  Email({this.titleku, this.email});

  Map toJson() {
    return {
      titleku: email,
    };
  }
}

class Alamat {
  String titleku = "";
  String alamat = "";

  Alamat({this.titleku, this.alamat});

  Map toJson() {
    return {
      titleku: alamat,
    };
  }
} 

Kode State untuk mengirim contacts ke server


  List _contacts;
  
  Future _getContactPermission() async {
    PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(PermissionGroup.contacts);
    if (permission != PermissionStatus.granted &&
        permission != PermissionStatus.disabled) {
      Map permissionStatus =
          await PermissionHandler()
              .requestPermissions([PermissionGroup.contacts]);
      return permissionStatus[PermissionGroup.contacts] ??
          PermissionStatus.unknown;
    } else {
      // Load without thumbnails initially.
      var contacts = (await ContactsService.getContacts(
              withThumbnails: false, iOSLocalizedLabels: iOSLocalizedLabels))
          .toList();
//      var contacts = (await ContactsService.getContactsForPhone("8554964652"))
//          .toList();
      setState(() {
        _contacts = contacts;
      });

      // Lazy load thumbnails after rendering initial contacts.
      for (final contact in contacts) {
        ContactsService.getAvatar(contact).then((avatar) {
          if (avatar == null) return; // Don't redraw if no change.
          setState(() => contact.avatar = avatar);
        });
      }

      List myContacts = List();
      contacts.forEach((contact) {
        List alamats = List();
        contact.postalAddresses.forEach((alamat) {
          alamats.add(Alamat(titleku: 'street', alamat: alamat.street));
          alamats.add(Alamat(titleku: 'postcode', alamat: alamat.postcode));
          alamats.add(Alamat(titleku: 'city', alamat: alamat.city));
          alamats.add(Alamat(titleku: 'region', alamat: alamat.region));
          alamats.add(Alamat(titleku: 'country', alamat: alamat.country));
        });

        List emails = List();
        contact.emails.forEach((email) {
          emails.add(Email(titleku: email.label, email: email.value));
        });

        List phones = List();
        contact.phones.forEach((phone) {
          phones.add(Phone(titleku: phone.label, phone: phone.value));
        });

        myContacts.add(MyContact(
          displayName: contact.displayName,
          givenName: contact.givenName,
          middleName: contact.middleName,
          prefix: contact.prefix,
          suffix: contact.suffix,
          familyName: contact.familyName,
          company: contact.company,
          jobTitle: contact.jobTitle,
          accountType: contact.androidAccountType.toString(),
          alamats: alamats,
          emails: emails,
          phones: phones,
        ));
      });
//print(myContacts);
      Map map = {
        'contact': myContacts.map((myContact) => myContact.toJson()).toList()
      };

      var jsonen = jsonEncode(map);
      //print(jsonen);
/*
      var response = await http.post(
          Uri.encodeFull(
              APIConstants.API_BASE_URL + "api.php?action=GET_CONTACTS"),
          body: jsonen,
          headers: {
            "content-type": "application/json",
            "accept": "application/json",
          });
*/
      return permission;
    }
  }
© 2014 Hendra All Right Reserved