4
votes

In a Flutter application using cloud_firestore to access a Cloud Firestore document database, I am getting the following warnings/errors in the console when debugging ...

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<…>

I am trying to determine how to make these settings/configuration changes in the Flutter cloud_firestore package (which wraps the native packages).

I load the firestore object statically in the main application startup and add it to a redux middleware package that persists throughout the application. So I thought I would add something like this static function ...

  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;
  }

But I don't see the settings objects exposed here.

Does anyone know if there is a different place where these settings could be configured, or is this functionality not passed through yet in the Flutter/Dart implementation of cloud_firestore?

1
I posted an answer but it was deleted by a moderator because it was the same answer to another question. Apparently that's not allowed (!!!). Maybe I'm allowed to post a comment with a link here that'll help? github.com/flutter/flutter/issues/18159#issuecomment-431176341Danny Tuppeny

1 Answers

0
votes

You can set the settings as of v0.8.2 of the Firebase plugin:

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

It's done like this:

await firestore.settings(timestampsInSnapshotsEnabled: true);