Refresh DocumentSnapshot after setData() without additional query

Viewed 299

In order to set new data in a Firestore document, I normally use setData() with merge: true. This works fine and as intended regarding data saving:

await userDoc.reference.setData({
    'favourite_color': 'blue',
    'favourite_team': 'fcporto',
  },merge: true);

My only issue is that DocumentSnapshot (userDoc in this example) doesn't get automatically 'refreshed' after setData is finished, which forces me to do an additional query to the document.

Is there any way to automatically update DocumentSnapshot after setData() ? (same behaviour happens with updateData())

2 Answers

No, DocumentSnapshot objects are immutable - the can't be changed. You will have to make another query, or wait for another realtime update from a listener.

I have many places in my code where I have Listeners for document/documents, and I perform completely separate .set()s etc on them - the Listeners take care of updating my local copy of the document (whether by Redux or simply useState Hooks). Not really a good idea to hold onto the DocumentSnapshot(s) - copy them to some form of local state.

Related