I have the starts and ends fields in my model as Timestamp, but I am getting this error. I don't get it when I define start and end as var in my Model.
Unhandled Exception: type 'Null' is not a subtype of type 'Timestamp'
Model:
import 'package:cloud_firestore/cloud_firestore.dart';
class Event {
String eid;
String title;
String location;
Timestamp start;
Timestamp end;
String instructor;
String image;
String description;
Event({
required this.eid,
required this.title,
required this.location,
required this.start,
required this.end,
required this.instructor,
required this.image,
required this.description
});
factory Event.fromMap(Map<String, dynamic>? map) {
return Event(
eid: map?['eid'] ?? 'undefined',
title: map?['title'] ?? 'undefined',
location: map?['location'] ?? 'undefined',
start: map?['starts'],
end: map?['ends'],
instructor: map?['instructor'] ?? 'undefined',
image: map?['image'] ?? 'undefined',
description: map?['description'] ?? 'undefined'
);
}
start: map?['starts'],
end: map?['ends'],
What you are doing here is setting the start/end arguments to the map from firestore, but if the document doesn't have that field, you have no fallback and it returns null
.
Do one of either:
start: map?['starts'] ?? Timestamp.now(),
end: map?['ends'] ?? Timestamp.now(),
class Event {
// ...
Timestamp? start;
Timestamp? end;