我试图只返回在Flutter中的DateTime.now()之后发生的事件。我从云firestore中获取事件,并在event_date下声明了日期和时间。
这是我的代码:
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(),
),
);
},
),我得到的错误是:
Class 'Timestamp' has no instance method 'isAfter'.
Receiver: Instance of 'Timestamp'
Tried calling: isAfter(Instance of 'DateTime')任何帮助都将不胜感激,因为我以前从未使用过isBefore或isAfter,所以我有点挣扎。
发布于 2021-02-11 03:22:37
使用Timestemp的替代解决方案。它有以下公共方法:
public int compareTo (Timestamp other)并且还具有
static Timestamp now()所以,你需要检查
(document.data()['event_date']).compareTo(Timestamp.now())https://stackoverflow.com/questions/66142882
复制相似问题