firebase - 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<…>
我正在尝试确定如何在 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);
推荐阅读
- php - react & axios & PHP [在预检响应中 Access-Control-Allow-Headers 不允许请求标头字段 access-control-allow-origin。]
- yaml - 用新值替换 yq 的 Yaml 值作为 bash 函数的输出,该函数接受当前值作为输入
- html - 使用纯 CSS 的从右到左、从上到下的砌体布局?
- reactjs - 如何更改按钮颜色 onClick Material-UI, React
- ios - 确定 UILabel 的字体大小或比例
- python - 导入我自己的 java 文件时找不到 Pyjnius 类
- google-schemas - 我们是否需要为每个环境请求 Gmail Schema 白名单请求?
- javascript - “获取”事件侦听器未在服务人员中触发以通过 Safari 中的 multipart/form-data 发布文件上传
- assembly - FAT16 Bootloader 仅加载文件的第一个簇
- powershell - IISRESET 和 ROBOCOPY 等某些命令在 Powershell v4 中已停止工作