[英]How to fill Map from DocumentSnapshot in Using Firestore in Flutter?
[英]How do you get a documentSnapshot from firestore over a cloud function in flutter?
首先介紹一下,我需要獲取 documentSnapshot 而不僅僅是 Firestore 中記錄的數據,因為我的應用程序中有分頁,為此,當您查詢時,您需要從您的位置發送最后一個 documentSnapshot查詢將發送下 10 條記錄。
因此,這在 SDK 上運行良好,但我現在想實現 Cloud Functions,當我嘗試使用來自雲函數的 documentSnapshot 進行響應時,我無法在 Flutter 中獲取 documentSnapshot。
這是問題所在的代碼部分:
final function = _cloudFunctions.getHttpsCallable(
functionName: 'createRecord',
);
try {
final response = await function.call(data);
final DocumentSnapshot doc = response.data;
return doc;
} catch (e) {
print(e)
return null;
}
通過這段代碼,我得到了錯誤:type '_InternalLinkedHashMap' is not a subtype of type 'DocumentSnapshot'
有解決方法還是我做錯了什么?
來自可調用 Cloud Function 的響應不會是強類型的DocumentSnapshot 。 這就是錯誤消息試圖告訴您的內容。 你得到一個地圖類型的對象,你必須這樣處理這個對象。 您不能只強制它成為 DocumentSnapshot。
如果您將可調用函數用作分頁工具,您將無法在客戶端和函數之間來回傳遞 DocumentSnapshot 對象。 您將必須向函數傳遞足夠的信息以在不提供 DocumentSnapshot 的情況下進行分頁。
分頁實際上不需要 DocumentSnapshot - 您需要的是足夠的信息來告訴查詢從中斷的地方開始,如文檔中所示。 這涉及使用 startAt 或 startAfter 使用上次看到的文檔中的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.