I have a list of contacts which I am trying to load into a ListView, using the contacts_service plugin. For each Contact, I want to check first if they have been marked as isFavourite
, determined by checking if their ID is in a list in SharedPreferences
. If so, then we show a star next to their name.
Because checking SharedPreferences requires a Future, how do I accomplish this? Progress so far is below.
Contacts Screen:
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: contacts?.length,
itemBuilder: (BuildContext context, int index) {
Contact c = contacts?.elementAt(index);
return ListTile(
// Show star next to Contact's name
leading: c.isFavourite ? Icon(Icons.star) : null,
title: Text(c.name);
);
}
);
);
}
Shared Preferences:
static Future<bool> checkIsFavourite(String contactId) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> favouriteContacts = prefs.getStringList("Favourites");
return favouriteContacts.contains(contactId);
}
Contacts Service:
class ContactsService {
static const MethodChannel _channel =
MethodChannel('github.com/clovisnicolas/flutter_contacts');
/// Fetches all contacts, or when specified, the contacts with a name
/// matching [query]
static Future<Iterable<Contact>> getContacts(
{String query, bool withThumbnails = true}) async {
Iterable contacts = await _channel.invokeMethod('getContacts',
<String, dynamic>{'query': query, 'withThumbnails': withThumbnails});
return contacts.map((m) => Contact.fromMap(m));
}
Contact class:
class Contact {
Contact(
{this.givenName,
this.middleName,
this.prefix,
this.suffix,
this.familyName,
this.company,
this.jobTitle,
this.emails,
this.phones,
this.postalAddresses,
this.avatar,
this.note,
this.isSelected});
String identifier,
displayName,
givenName,
middleName,
prefix,
suffix,
familyName,
company,
jobTitle,
note;
Iterable<Item> emails = [];
Iterable<Item> phones = [];
Iterable<PostalAddress> postalAddresses = [];
Uint8List avatar;
bool isSelected;
String initials() {
return ((this.givenName?.isNotEmpty == true ? this.givenName[0] : "") +
(this.familyName?.isNotEmpty == true ? this.familyName[0] : ""))
.toUpperCase();
}
Contact.fromMap(Map m) {
identifier = m["identifier"];
displayName = m["displayName"];
givenName = m["givenName"];
middleName = m["middleName"];
familyName = m["familyName"];
prefix = m["prefix"];
suffix = m["suffix"];
company = m["company"];
jobTitle = m["jobTitle"];
emails = (m["emails"] as Iterable)?.map((m) => Item.fromMap(m));
phones = (m["phones"] as Iterable)?.map((m) => Item.fromMap(m));
postalAddresses = (m["postalAddresses"] as Iterable)
?.map((m) => PostalAddress.fromMap(m));
avatar = m["avatar"];
note = m["note"];
isSelected = SharedPreferences.checkIsFavourite(m["identifier"]);
// What is the correct way to check if it's a favourite?
}