0

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?
    }
user2181948
  • 1,646
  • 3
  • 33
  • 60

1 Answers1

2

I would recommend defining an app-wide super variable that initializes on app launch.

e.g. main.dart

SharedPreferences prefs;

void main() async {
  prefs = await SharedPreferences.getInstance();
  return runApp(MyApp());
}

Then you can pass the prefs variable to any other screens / widgets where it's needed in form of constructor argument. e.g.

class ContactsScreen extends StatefulWidget {
  ContactsScreen({
    Key key,
    @required this.prefs,
  }):
    super(key: key);

  final SharedPreferences prefs;

  ...
}

You might have noticed in your code that SharedPreferences initialization is being called every time a contact is being built in ListView, followed by prefs.getStringList("Favourites") call.

These 2 are relatively heavy operations that can be avoided if you define favourites array once and use it for every contact build call.

How I would do that is create isFavourite function in Contact object that accepts List<String> as an argument. e.g.

class Contact {
  ...
  bool isFavourite(List<String> favouriteIds) {
    assert(favouriteIds != null);

    return favouriteIds.contains(this.identifier);
  }
  ...
}

and in Contacts Screen build:

final List<String> favouriteIds = prefs.getStringList("Favourites") ?? []; // Use empty array in case `Favourites` is not set
...
ListTile(
  // Show star next to Contact's name
  leading: contact.isFavourite(favouriteIds) ? Icon(Icons.star) : null,
  title: Text(contact.name);
);
...

However, I have to warn you that I am not aware of how global (/ super / app-wide) variables are treated in Dart / Flutter and whether it's a good idea to declare main() method as async, so use it at your own risk.(updated)

From my personal experience - I have not encountered any problems so far.

UPDATED

Turns out declaring main() as async if perfectly safe.

As for global variables - my method is OK as it is, though there is an even better one.

George Zvonov
  • 9,401
  • 5
  • 33
  • 37