Contact Data

To make the example as simple as possible, ContactsList constructor will take data created directly in the code (hardcoded). How to fetch data using services will be explained in the next article.

First thing to be done is creating a file called contact_data.dart at the same level the main.dart file is, inside lib folder.

Next step is defining a class to specify the contact data, i.e: name, email, phone number, address, etc… inside contact_data.dart file.

It is a simple class where a contact data is defined. Using {} to make the parameters the constructor takes as optional allows to reference them by name. What is more, constructor is defined as a const in order to create instance constants in compilation time.

Next step is creating a sample contacts list, the example below represents a list populated with two contacts.

Take a look to the first sentence, the left side const kContacts, defines a constant which can´t be reassigned. In the right side, const <Contact> [] defines an immutable list. Inside this list a Contact instance must be created for each existing contact. By the time we want a new contact to be created we use const Contact instead of new Contact so it becomes a constant at compilation time. As you can see, marking Contact class attributes as optional allows to assign them by name.