首页 > 解决方案 > Flutter cloud_firestore 系统日期对象的设置/警告

问题描述

在使用 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<…&gt;

我正在尝试确定如何在 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 实现中还没有通过吗?

标签: firebasedartgoogle-cloud-firestoreflutter

解决方案


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

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

它是这样完成的:

await firestore.settings(timestampsInSnapshotsEnabled: true);

推荐阅读