飘散cloud_firestore系统日期对象的设置/警告

时间:2018-05-24 13:16:07

标签: firebase dart google-cloud-firestore flutter

在使用cloud_firestore访问Cloud Firestore文档数据库的Flutter应用程序中,我在调试时在控制台中收到以下警告/错误...

4.13.0 - [Firebase/Firestore][I-FST000001] The behavior for system Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:

let db = Firestore.firestore()
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings

With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:

// old:
let date: Date = documentSnapshot.get("created_at") as! Date
// new:
let timestamp: Timestamp = documentSnapshot.get("created_at") as! Timestamp
let date: Date = timestamp.dateValue()

Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you d<…>

我正在尝试确定如何在Flutter cloud_firestore包中进行这些设置/配置更改(包装本机包)。

我在主应用程序启动时静态加载firestore对象,并将其添加到在整个应用程序中持续存在的redux中间件包。所以我想我会添加类似静态函数的东西......

  static Firestore getStartupFirestore() {
    var fs = Firestore.instance;

// #TODO: adapt this code to Dart for Flutter implementation
// let db = Firestore.firestore()
// let settings = db.settings
// settings.areTimestampsInSnapshotsEnabled = true
// db.settings = settings

    return fs;
  }

但是我没有看到这里公开的设置对象。

有没有人知道是否有可以配置这些设置的不同位置,或者是否在cloud_firestore的Flutter / Dart实现中尚未传递此功能?

1 个答案:

答案 0 :(得分:0)

您可以从Firebase插件v0.8.2开始设置设置:

https://github.com/flutter/flutter/issues/18159#issuecomment-431176341

它是这样完成的:

await firestore.settings(timestampsInSnapshotsEnabled: true);