【问题标题】:Flutter Firebase TimeStampFlutter Firebase 时间戳
【发布时间】:2020-05-20 14:10:17
【问题描述】:

我想显示在今天或上周创建的帖子。我怎样才能做到这一点。

这是我的 Firebase 数据

这是我的查询

  Future<List<Post>> getAllPost(Post lastFetched, int fetchLimit) async {
    List<Post> _postList = [];
    if (lastFetched == null) {
      _querySnapshot = await Firestore.instance
          .collection("posts")
          .orderBy("liked", descending: true)
          .limit(fetchLimit)
          .getDocuments();
    } else {
      _querySnapshot = await Firestore.instance
          .collection("posts")
          .orderBy("liked", descending: true)
          .startAfterDocument(lastDocument)
          .limit(fetchLimit)
          .getDocuments();
    }
    if (_querySnapshot.documents.length != 0) {
      lastDocument =
          _querySnapshot.documents[_querySnapshot.documents.length - 1];
    }

    for (DocumentSnapshot documentSnapshot in _querySnapshot.documents) {
      Post tekPost = Post.fromMap(documentSnapshot.data);
      _postList.add(tekPost);
    }
    return _postList;
  }

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:
    var startfulldate = admin.firestore.Timestamp.fromDate(new Date(1556062581000));
    db.collection('mycollection')
      .where('start_time', '<=', startfulldate)
      .get()
      .then(snapshot => {              
            var jsonvalue: any[] = [];
            snapshot.forEach(docs => {
              jsonvalue.push(docs.data())
               })
                 res.send(jsonvalue);
                 return;
                }).catch( error => {
                    res.status(500).send(error)
                });
    

    【讨论】:

      【解决方案2】:

      有一个库调用timeago

      对于创建模糊时间戳很有用。 (例如“5 分钟前”)

      import 'package:timeago/timeago.dart' as timeago;
      
      main() {
         final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));
      
         print(timeago.format(fifteenAgo)); // 15 minutes ago
         print(timeago.format(fifteenAgo, locale: 'en_short')); // 15m
         print(timeago.format(fifteenAgo, locale: 'es')); // hace 15 minutos
      }
      

      【讨论】:

      • 但我需要在我的 firebase 搜索查询中进行配置。例如,如果帖子创建于 1 天前,则获取文档,如果没有获取。
      • fetchLimit 的时间是否以毫秒为单位?例如,今天是 1589986642126
      猜你喜欢
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 2019-01-19
      • 2019-02-08
      • 2021-11-23
      • 2020-11-04
      • 2016-12-13
      • 1970-01-01
      相关资源
      最近更新 更多