I am currently working on a calorie-tracking app that uses Firestore.
I used Firestore to store food items in a document. However, some of these food items contain restricted characters (‘~’, ‘*’, ‘/’, ‘[‘, or ‘]’) that are not allowed in the field path.
For instance, this map that contains the food items. Note the use of the restricted character.
Is there a recommended way of handling the use of restricted characters? I’m thinking of encoding and decoding the strings before updating Firestore and decoding them when getting the documents, by replacing all restricted characters with specific strings.
Appreciate the help! Thank you!
2
Answers
Encode:
decode:
Yes, that’s correct, when working with Firestore, there are certain characters like
('~', '*', '/', '[', or ']')
which are not allowed to be used as keys in documents.While encoding and decoding the strings might solve the problem, there is another way of handling this situation. So instead of using those strings as keys you can add them as string values, where there are no restrictions. For instance, let’s assume you need to have a key called
AC/DC
, which obviously cannot be used as a key because it contains a forward slash/
. So instead of using:You can use a Map and store the info like this:
In this way, you also be able to map the the Firestore document into an object of type
RockBand
for example.