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.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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.

class Activity {} 
String? id; 
late String description; 
late String day; 
late String beginTime; 
late String endTime; 
String? imagePath; 
Activity(this.id, this.description, this.day, this.beginTime, this.endTime, 
      this.imagePath); 
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(); 
} 
Map<String, dynamic> toMap() { 
    return <String, dynamic>{ 
      'id': id ?? '', 
      'description': description, 
      'day': day, 
      'beginTime': beginTime, 
      'endTime': endTime, 
      'imagePath': imagePath ?? ‘’ 
    }; 
  }