Reputation: 308
I am trying to return only events that are happening after DateTime.now()
in Flutter. I am taking the events from cloud firestore and have declared the date and time under event_date
.
This is my code:
new StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Events')
.orderBy('event_date', descending: false)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Text('Loading data... Please Wait');
return new Container(
height: 100.0,
child: new ListView(
scrollDirection: Axis.horizontal,
children: snapshot.data.documents.map<Widget>((DocumentSnapshot document) {
if ((document.data()['event_date']).isAfter(now)) {
return new Card(
color: Color(0xff004FB2),
child: new Container(
width: 100.0,
height: 100.0,
child: new Text(document.data()['title']),
),
);
} else {
return Container();
}
}).toList(),
),
);
},
),
The error I am getting is:
Class 'Timestamp' has no instance method 'isAfter'.
Receiver: Instance of 'Timestamp'
Tried calling: isAfter(Instance of 'DateTime')
Any help would be appreciated as I have never used the isBefore
or isAfter
before so I am struggling a bit.
Upvotes: 0
Views: 1733
Reputation: 498
Alternative solution using Timestemp. It has the following public method:
public int compareTo (Timestamp other)
and also has
static Timestamp now()
so, you need to check
(document.data()['event_date']).compareTo(Timestamp.now())
Upvotes: 0
Reputation: 3084
Try first converting your Timestamp
object into a DateTime
object
(document.data()['event_date']).toDate().isAfter(now)
or:
(document.data()['event_date']).toDate().isAfter(now.toDate())
let me know if it worked
Upvotes: 1