firebase - Flutter cloud_firestore 系统日期对象的设置/警告

标签 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 实现中传递?

最佳答案

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

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

这样做是这样的:

await firestore.settings(timestampsInSnapshotsEnabled: true);

关于firebase - Flutter cloud_firestore 系统日期对象的设置/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50510396/

相关文章:

javascript - 无法在 firestore 数据库中添加多个字段,但在添加单个字段时工作正常

javascript - Firebase limitToFirst 无法按预期工作

firebase - Firebase 动态链接是否有使用配额?

dart - 尝试从列表中删除对象时迭代期间的并发修改

javascript - Node.js Firestore 身份验证问题(当 javascript 工作时)

firebase - 如何使用表单验证通过Flutter验证集合Firestore中是否存在文档

ios - 如何快速从firebase实时数据中获取特定值的数据?

javascript - Firebase 和 JavaScript - 如何更新具有特定 ID 值的对象?

flutter - 如何在Flutter中替换GridView中的特定项目?

flutter - TextFormField 值为空