Reputation: 303
I have called for the data of fieldunion array
Future _getData(String email) async {
await FirebaseFirestore.instance
.collection("shop")
.get()
.then((querySnapshot) {
querySnapshot.docs.forEach((result) {
print(result.data()["products"]);
});
});
}
This is the data I get which is the fieldunion array of products.
[{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}
Now, I want to specifically get the value of one of it's attribute which is category. How do do that?
result.data()["products"][2]["category"]
is not working.
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError (index): Invalid value: Not in inclusive range 0..1: 2
Upvotes: 0
Views: 359
Reputation: 21
Be sure that the data return when you do result.data()["products"] is of type List if not use the jsonDecode method to convert it into a List In order to allows you get access to field of that List
Now after being sure for the above things
So if it a string do try this:
dynamic results=jsonDecode (result.data()["products"]); String category=results [0]["category"];
If it is already a List directly you can access your category field like that: result.data()["products"][0]["category"]
Upvotes: 2