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

Posted

技术标签:

【中文标题】Flutter cloud_firestore 系统日期对象的设置/警告【英文标题】:Flutter cloud_firestore settings/warnings for system date objects 【发布时间】:2018-11-03 17:50:27 【问题描述】:

在使用 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 实现中传递?

【问题讨论】:

我发布了一个答案,但它被版主删除了,因为它与另一个问题的答案相同。显然这是不允许的(!!!)。也许我可以在这里发表带有链接的评论,这会有所帮助? github.com/flutter/flutter/issues/18159#issuecomment-431176341 【参考方案1】:

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

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

这样做是这样的:

await firestore.settings(timestampsInSnapshotsEnabled: true);

【讨论】:

以上是关于Flutter cloud_firestore 系统日期对象的设置/警告的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:cloud_firestore 的构建错误:高于版本“0.7.4”

Flutter (iOS) - 在 GeneratedPluginRegistrant.m 中找不到模块“cloud_firestore”

I/flutter (22027):MissingPluginException(在通道 plugins.flutter.io/cloud_firestore 上找不到方法 DocumentRefer

Flutter 中的 cloud_firestore 和 firebase_auth 兼容性问题

添加依赖项 cloud_firestore 时,新的 Flutter 应用程序失败:^1.0.7

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