首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类“Timestamp”没有实例方法“isAfter”Flutter

类“Timestamp”没有实例方法“isAfter”Flutter
EN

Stack Overflow用户
提问于 2021-02-11 02:28:00
回答 2查看 363关注 0票数 0

我试图只返回在Flutter中的DateTime.now()之后发生的事件。我从云firestore中获取事件,并在event_date下声明了日期和时间。

这是我的代码:

代码语言:javascript
运行
复制
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(),
                ),
            );
        },
    ),

我得到的错误是:

代码语言:javascript
运行
复制
Class 'Timestamp' has no instance method 'isAfter'.
Receiver: Instance of 'Timestamp'
Tried calling: isAfter(Instance of 'DateTime')

任何帮助都将不胜感激,因为我以前从未使用过isBeforeisAfter,所以我有点挣扎。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-11 03:10:10

首先尝试将Timestamp对象转换为DateTime对象

代码语言:javascript
运行
复制
(document.data()['event_date']).toDate().isAfter(now)

或者:

代码语言:javascript
运行
复制
(document.data()['event_date']).toDate().isAfter(now.toDate())

让我知道它是否有效

票数 1
EN

Stack Overflow用户

发布于 2021-02-11 03:22:37

使用Timestemp的替代解决方案。它有以下公共方法:

代码语言:javascript
运行
复制
public int compareTo (Timestamp other)

并且还具有

代码语言:javascript
运行
复制
static Timestamp now()

所以,你需要检查

代码语言:javascript
运行
复制
(document.data()['event_date']).compareTo(Timestamp.now())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66142882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档