我從 Firebase 獲取數(shù)據(jù)作為模型,在我的模型中,有一個(gè)List<String>所以我可以將其發(fā)送到 Firebase 但無法獲取:(我有一個(gè)模型,其中圖像是列表圖像;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"), }這給出了一個(gè)錯(cuò)誤,說我在 List 的 sted 中獲取對(duì)象“document.get(“image”)”或字符串“document.getString(“image”)”沒有“document.getList(“image”)”
1 回答

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