簡體   English   中英

如果數組包含字符串,是否有一種使用 firestore 的方法將使用 flutter 打印消息

[英]Is there a way in using firestore that will print a message using flutter if an array contains a string

如果數組包含字符串,是否有一種使用 firestore 的方法將使用 flutter 打印消息。

我想檢查特定文檔中的數組是否包含用戶 email。 如果數組包含用戶 email,它將顯示一條消息。

這是我當前的代碼,但它不起作用。

(FirebaseFirestore.instance.collection('users').where(
                                'requests',
                                arrayContains: user.email)  == true)
                        ? Text("true")
                        : Text("false");

從 firestore 獲取用戶電子郵件列表並存儲在本地列表 [listName] 中。 然后調用 listName.contains(userEmail) 並將結果存儲在 bool 變量中。 像這樣,

  bool isEmailexist =  usersList.contains(userEmail);

最后,您可以通過檢查 bool 值來顯示您的消息。

您擁有的代碼僅聲明了一個查詢。 它還沒有執行那個查詢,所以它還沒有辦法檢查是否有任何結果。

要執行查詢,您需要調用get() (如獲取數據一次的文檔中所示)或snapshots() (如有關偵聽更新的文檔中所示)。

這兩個操作都是異步的,所以你必須在你的小部件中處理它。 對於返回Futureget() ,您可以使用FutureBuilder ,而對於返回Streamsnapshots ,您可以使用StreamBuilder

暫無
暫無

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

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