gpt4 book ai didi

firebase - 如何从Firebase Flutter获取fieldValue.arrayUnion数据

转载 作者:行者123 更新时间:2023-12-03 04:17:33 24 4
gpt4 key购买 nike

我已经要求fieldunion数组的数据

Future _getData(String email) async {
await FirebaseFirestore.instance
.collection("shop")
.get()
.then((querySnapshot) {
querySnapshot.docs.forEach((result) {
print(result.data()["products"]);
});
});
}
这是我得到的数据,是产品的fieldunion数组。

[{img: [https://firebasestorage.googleapis.com/v0/b/geniemart-387b1.appspot.com/o/seller%2Fproduct%2Frohanchoudhary2000%40gmail.com%2F20%2Fimage_1?alt=media&token=6b67a55d-01aa-4970-b518-719c30db7938, https://firebasestorage.googleapis.com/v0/b/geniemart-387b1.appspot.com/o/seller%2Fproduct%2Frohanchoudhary2000%40gmail.com%2F20%2Fimage_2?alt=media&token=483b46e0-9d8f-48ec-b431-d083341d4815, https://firebasestorage.googleapis.com/v0/b/geniemart-387b1.appspot.com/o/seller%2Fproduct%2Frohanchoudhary2000%40gmail.com%2F20%2Fimage_3?alt=media&token=fea76365-59cd-47d2-96a4-55a9387dfbf5, https://firebasestorage.googleapis.com/v0/b/geniemart-387b1.appspot.com/o/seller%2Fproduct%2Frohanchoudhary2000%40gmail.com%2F20%2Fimage_4?alt=media&token=a2136333-d099-4465-8121-2cf9f2e75d61], mprice: 123, freq: [], discount: 12, marketedBy: Manufacturer, long: 72.0, life: 5 years, manufacturer: Manufacturer, isveg: true, oprice: 123, name: tgbbb, id: 10000000, category: Tomato , brand: tgbbb, lat: 28.0, desc: gyyhhh}


现在,我要专门获取其属性之一即类别的值。怎么做? result.data()["products"][2]["category"]无法正常工作。
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError (index): Invalid value: Not in inclusive range 0..1: 2

最佳答案

如果不使用jsonDecode方法将其转换为List,请确保执行result.data()[“products”]时返回的数据为List类型。
为了使您能够访问该列表的字段
现在确定以上内容之后
因此,如果是字符串,请尝试以下操作:
动态结果= jsonDecode(result.data()[“产品”]]);
字符串category = results [0] [“category”];
如果已经直接是列表,则可以像这样访问类别字段:
result.data()[“产品”] [0] [“类别”]

关于firebase - 如何从Firebase Flutter获取fieldValue.arrayUnion数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64664998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com