Justin Yu
Justin Yu

Reputation: 53

Is there a converter method for the object in firestore in flutter?

I am trying to find a way to convert my customized object in order to send it to firestore. I tried to look at the Firestore documents but found nothing referencing to flutter. Is there a way to convert the object and send it to firestore?

Upvotes: 2

Views: 515

Answers (1)

Christopher Moore
Christopher Moore

Reputation: 17113

As Doug Stevenson says in his answer here, you can store raw data in firestore documents. For specifically flutter, you have to create a Blob from your object. See this for what a blob is. To convert to the Uint8List that the Blob constructor takes, see this answer converting Dart objects into a JSON string and converting this string into a byte array. It doesn't seem like a great method, but I personally don't know any alternatives. The basic gist of the answer is to serialize the object to a JSON string.

Alternatively, if possible, you can create a conversion method that stores your object as a Map and write that to your document. This reference shows a javascript example, but shows a similar concept.

Upvotes: 3

Related Questions