Thinking & coding: Building the event data model in my application.

In today's programming session, I'm building the event page model. 

I want to build this app where the business logic is seperated from the UI. So after this model I have to build a library to handle the connection with the backend. This two entities will be connected with a middle layer, in our case a Provider.

And all this is again with the horizon to make this app scalable, expandable and easy to maintain. I have build this model inside a seperate dart file.

When I look at my app prototype, what information do I need. I have two screens related to the events. The first screen, 'events screen' is a 'list of events'. In this list, each event will be displayed in a 'event card'. And the 'event detail screen' can be called up from in this card.

The second screen is therefore the 'event detail screen'. All details of the event will be displayed here.


What different data do I need in the screens.


The EventsCard (in List): 

  • eventId (not displayed)
  • eventTitle
  • eventDate
  • eventSubTitle
  • eventImage.
The EventsDetailScreen.
  • eventId (not displayed)
  • eventTitle
  • eventDate
  • eventText
  • eventSubTitle
  • eventImage.


So what does this list of data elements show. In total I need 6 data elements. In both screens there are 4 common data elements. So that's definitely a stroke of luck.


Just to mention, to build this model I follow a work method of Andy Julow. Who recently made a very clear update video in this matter. In which a few significant changes occur in the current method of implementing FireBase in Flutter.


The code and the problems I encountered.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// defining the event data model class with the name Event
// Events has to be the collection name inside Firestore collections


class Events {
  final String eventId;
  final String eventTitle;
  final String eventDate;
  final String eventSubTitle;
  final String eventText;
  final String eventImage;

  Events(
      {this.eventId,
      this.eventTitle,
      this.eventDate,
      this.eventSubTitle,
      this.eventText,
      this.eventImage});

  // json from firestore to dart
  factory Events.fromJson(Map<String, dynamic> json) {
    return Events(
      eventId: json['eventId'],             /// 'eventId' got to be the field name in database
      eventTitle: json['eventTitle'],       /// same for all other lines
      eventDate: json['eventDate'],
      eventSubTitle: json['eventSubtitle'],
      eventText: json['eventText'],
      eventImage: json['eventImage'],
    );
  }

  // dart map to firestore json
  Map<String, dynamic> toMap() {
    return {
      'eventId': eventId,
      'eventTitle': eventTitle,
      'eventDate': eventDate,
      'eventSubTitle': eventSubTitle,
      'eventText': eventText,
      'eventImage': eventImage
    };
  }
}


There where no problems with building this model. Just make sure to use the correct collection end document names in the backend. Firebase is lower and upper case sensitive, I noticed ;-)

Thank you Andy Julow for the fantastic video's on this matter on your Youtube Channel.

Do,believe and be happy

Stefaan




Comments

  1. Hi there, great article as always :),

    I have one question, is your eventDate really from type String or more DateTime?

    ReplyDelete
    Replies
    1. Hi Max, thanks for taking your time to read and comment on my post. Yes, the eventDate is indeed a String. And this for 2 reasons. The first reason is because the date will simply be displayed in the EventCard in the list of Events. The second reason is that some events last several days or weeks. So a start and end date.
      For now I am not going to rank the events by date yet. I want to do that later for sure. Whats your idea in this?

      Delete

Post a Comment

Popular posts from this blog

Thinking & coding: Linking Firebase to my Flutter App. Watch out for a important detail.

Software developer yes, but where to begin?

Thinking & coding: Small but important code implementations.