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'
);
}
3
Answers
You are getting
null
value forstart
andend
, you need to define a default value for them like other variable: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:
This is because you are not giving a default value (the same way you are doing for all other variables):
?? 'undefined'
One possible solution is to allow for
null
values… just add?
for both variables definitions, like this:Let me know if this does not work.