我從 Firebase 獲取數據作為模型,在我的模型中,有一個List<String>所以我可以將其發送到 Firebase 但無法獲取:(我有一個模型,其中圖像是列表圖像;CollectionReference colRef = db.collection("Vehicle");colRef.get().addOnCompleteListener(task -> {for (QueryDocumentSnapshot document : task.getResult()) { Vehicle vehicle = new Vehicle(document.getString("title"), document.getString("city"), document.get("image"), }這給出了一個錯誤,說我在 List 的 sted 中獲取對象“document.get(“image”)”或字符串“document.getString(“image”)”沒有“document.getList(“image”)”
1 回答

米脂
TA貢獻1836條經驗 獲得超3個贊
沒有“document.getList(“image”)”
你是對的, QueryDocumentSnapshot類中沒有getList()
方法,但因為它擴展了DocumentSnapshot類,所以你可以使用get(String field)方法:
返回該字段的值,如果該字段不存在,則返回 null。
因此,如果您的image
屬性是數組類型,您可以List
使用以下代碼行簡單地獲取它:
List<String>?vehicleList?=?(List<String>)?document.get("image");
現在您可以Vehicle
通過傳遞vehicleList
給構造函數來創建類的對象。
添加回答
舉報
0/150
提交
取消