0

I'm trying to save a string with some special characters and when I try to do it, my firestore saves it as a map. Here is what I mean:

Future<Null> setSeen(bool seen, bool existent) async {
  print(episode['title']);
  //Output: Episódio 5.5 
  Map<String, dynamic> episodeData = {episode['title'].toString(): seen};
  if (existent) {
    await Firestore.instance
        .collection('users')
        .document(uid)
        .collection('seen')
        .document(title)
        .updateData(episodeData);
  } else {
    await Firestore.instance
        .collection('users')
        .document(uid)
        .collection('seen')
        .document(title)
        .setData(episodeData);
  }
}

The code above saves my data like this:

Screenshot of what is being saved on firestore

And this is how I wanted to save:

enter image description here

Vitor
  • 762
  • 6
  • 26
  • The `.` in your field name is interpreted as a request to access nested field. You can prevent this by wrapping the string in a `FieldPath` object as shown here: https://stackoverflow.com/a/53856542, but I don't immediately see how to do that in [FlutterFire](https://pub.dev/documentation/cloud_firestore_platform_interface/latest/cloud_firestore_platform_interface/FieldPath-class.html). – Frank van Puffelen May 04 '20 at 14:48
  • Try to put '\' before the dot – israel May 04 '20 at 14:49

1 Answers1

2

Unfortunately, it's not possible to directly add data with some special characters to firestore. The . operator for instance, in your case, results in a separate nested object instead of being a regular string. One possible solution is to use FieldPath to supply keys but unfortunately, since your question is tagged for Flutter, there is no direct support for it in the firestore plugin.

The other possible solution is to replace the special characters with some other allowed character. In your case, for instance you can replace them with a different set of valid characters like DOT or %24 or some other number to help you identify exactly which character has been replaced and parse it back while fetching.

Here's a link to a similar thread for RTDB if that helps.

Ashutosh Singh
  • 1,107
  • 17
  • 28
  • 1
    That link was really helpful, I just did a function to replace the key characters of firebase and it's now working as a charm. Thanks! – Vitor May 04 '20 at 14:52