Beginning FlutterFire

Aug 30 2022 · Dart 2.16, Flutter 3.0, Visual Studio Code 1.69

Part 3: Read & Write Data with the Cloud Firestore

10. Create the Model Class

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Add Data in a Firestore Collection Next episode: 11. Insert & Retrieve Data from Firestore

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 10. Create the Model Class

A rather common approach when dealing with databases, or web services, is creating classes that mirror the data you want to read and write: these classes are generally called models. Now you will create a model for the activities that you will save into a Firestore collection, and later retrieve from it.

So, in the data folder of the project, create a new file, called activity.dart. This contains a class, that you can call Activity.

class Activity {} 

For the fields of the class, create a nullable String, called id. It contains the id for the Document that we will save into the Firestore Collection, and it’s nullable because the id will be generated only after saving the Activity into the database. Next, let’s create a late string for the description, and another String for the day.

For the time we need to fields: one for the begin time, and one for the end time. We’ll mark those late as well. Finally, let’s create a nullable String that contains the path for an image that you’ll be able to add to your activity: this might be a picture that you take during or after completing your activity.

String? id; 
late String description; 
late String day; 
late String beginTime; 
late String endTime; 
String? imagePath; 

Once the fields are set, we can create an unnamed constructor, that sets all the fields: so, create an Activity constructor, that in its parameters takes and sets the id, description, day, begintime, endtime and imagePath of the activity.

Activity(this.id, this.description, this.day, this.beginTime, this.endTime, 
      this.imagePath); 

When you save a document into a Firestore collection, or you retrieve it from the database, you use key value pairs: it’s like the Json format: you have a String for the key, and a dynamic value. In Dart you create key value pairs using Map objects. So, let’s create a named constructor that takes a Map and creates an instance of Activity.

Call this constructor “fromMap”. This takes a Map of String, dynamic, called map, and a String for the id. In the constructor just set all the class fields: so, the id will take the id String that was passed.

The description takes the value of map description key. Let’s also append the toString method here. Repeat the same process for the remaining fields: the day field takes the value at map, day key.

BeginTime takes map[beginTime] toString, endtime takes map endtime tostring, and finally imagePath takes map imagePath tostring.

Activity.fromMap(Map<String, dynamic> map, String id) { 
  id = id; 
  description = map['description'].toString(); 
  day = map['day'].toString(); 
  beginTime = map['beginTime'].toString(); 
  endTime = map['endTime'].toString(); 
  imagePath = map['imagePath']?.toString(); 
} 

You’ll use the fromMap constructor when retrieving data from the database: Firestore supplies a Map, and you transform it into an Activity object. But what happens when you want to write data to the database? In this case you need to transform an Activity into a Map. So, let’s create a method to achieve this. This returns a Map of type String dynamic. Just call it “toMap”.

Here, return an object, that sets the id key with the id of the instance. If this is null, just return an empty string. The description takes the instance description, that’s non nullable.

The day takes the instance day, the beginTime the instance beginTIme, the endTIme the instance endTime, and finally the imagePath take the instance imagePath OR an empty string if it’s null.

Map<String, dynamic> toMap() { 
    return <String, dynamic>{ 
      'id': id ?? '', 
      'description': description, 
      'day': day, 
      'beginTime': beginTime, 
      'endTime': endTime, 
      'imagePath': imagePath ?? ‘’ 
    }; 
  } 

Great, the activity class is now ready. You’ll use it to interact with the Firestore database next!