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.
- eventId (not displayed)
- eventTitle
- eventDate
- eventText
- eventSubTitle
- eventImage.
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 }; } } |
Hi there, great article as always :),
ReplyDeleteI have one question, is your eventDate really from type String or more DateTime?
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.
DeleteFor 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?