简体   繁体   中英

Firestore and flutter: update documents when a field is null

I have a collection which represents a list of available sport matches (see image below, sorry for the italian text). Each document is a match, and has a list of players which are subscribed to that match (id_player1, id_player2, etc).

When someone would like to subscribe to that match, I have to cycle through the players_id, and when I find a null one, I set it to the user's id.

So my questions are:

  • how can I cycle through the fields of the document and check if they are null or not?
  • how can I count how many fields are not null, so when this count is equal to X, I do something?

在此处输入图像描述

You decide to define 6 different fields to store players id.. so u cannot cycle that fields.. what you can do is to get all of the six fileds and check one by one if they are null...

what you should do is to refactor that logic and store players id in a collection.. an update the collection only if its count is under 6 so you haven't t check if you have any space left to add player id

Bye:D

If there is no specific meaning to each individual id_player* field, consider storing all player IDs in a single player_ids array field.

That way you can use arrayUnion to add values to the field (preventing duplicates) and query with array_contains to find documents with a specific player ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM