簡體   English   中英

將字符串添加到 Firestore 中的數組 - flutter

[英]add string to an array in Firestore - flutter

我在 Firestore 數據庫中創建了一個字符串數組“challengeMember”,我想在用戶單擊按鈕時向它添加數據,我的數據庫:

在此處輸入圖像描述

在我的代碼中,我試圖更新數組,每次用戶加入挑戰時,挑戰名稱都會添加到這個數組中,我知道如何使用以下方法訪問數組:

FirebaseFirestore.instance.collection("Users").doc(user!.uid).update({'challengeMember': widget._challengeName});

但問題在於它沒有指定數組索引,也沒有檢查索引是否為空,所以我怎樣才能動態訪問這個數組並繼續添加值。

Firestore 有一個特殊的技巧來添加一個數組,它是這樣的:

await FirebaseFirestore.instance.collection('users').doc(uid).update({
        'tokens': FieldValue.arrayUnion([token]),
      });

有關如何操作 arrays 的信息,請參閱FieldValue文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM