How to parse dynamic JSON with Flutter?
802
It seems flutter
does not understand that jsonResponse
is an iterable
because you defined it as dynamic
.
Adjust the definition to tell flutter it is a map:
final Map<String, dynamic> jsonResponse = json.decode(jsonString);
Author by
Nairobi
Updated on December 06, 2022Comments
-
Nairobi 11 months
I do not understand how to parse a JSON file I get from firebase.
This is the format of the JSON file
{ "water" : { "-MnRJkFC3--ZOTmpF1xN" : { "milliliters" : 0.14, "time" : "16:26:25" }, "-MnRJkZRwZYEInHfSKIY" : { "milliliters" : 48.83, "time" : "16:26:25" }, "-MnRJksES18hY765rxxq" : { "milliliters" : 41.44, "time" : "16:26:25" }, "-MnRJlDn6o4RmiGRJS-E" : { "milliliters" : 11.37, "time" : "16:26:25" } } }
This is how I am reading the JSON file
Future loadSalesData() async { final String jsonString = await getJsonFromFirebase(); final dynamic jsonResponse = json.decode(jsonString); for (Map<String, dynamic> i in jsonResponse) chartData.add(SalesData.fromJson(i)); }
The getJsonFromFirebase() looks like this:
Future<String> getJsonFromFirebase() async { String url = "https://emailpassword. . .seio.com/water.json"; http.Response response = await http.get(Uri.parse(url)); return response.body; }
When you click on the link it send you to the JSON file which looks like this
{ "-Mnbk2ye2P8bfpaQvNaU": { "milliliters": 0.0, "time": "18:07:00" }, "-Mnbk6wd-wJze8P0JknK": { "milliliters": 0.12, "time": "18:07:00" }, "-Mnbk7Ek629vgBu-MiLg": { "milliliters": 44.91, "time": "18:07:00" }, "-Mnbk7bPuzqwsz9d5nm6": { "milliliters": 5.43, "time": "18:07:00" }, "-Mnbk7v7MADi7YzEbeFI": { "milliliters": 24.54, "time": "18:07:00" }, "-Mnbk8DGfqswckdsA1qP": { "milliliters": 47.58, "time": "18:07:00" }, "-Mnbk8Xw2kJPxLrqCl6h": { "milliliters": 13.98, "time": "18:07:00" } }
I get the Error
_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable'
-
Frank van Puffelen about 2 yearsWhat does
getJsonFromFirebase
look like? -
Nairobi about 2 yearsHi Frank, this is how the getJsonFromFirebase looks like Future<String> getJsonFromFirebase() async { String url = "emailpass. . . .firebaseio.com/water.json"; http.Response response = await http.get(Uri.parse(url)); return response.body; } The link sends you to JSON file on my realtime database
-
Nairobi about 2 yearsWhen you click the link the JSON file looks like this {"-Mnbk2ye2P8bfpaQvNaU":{"milliliters":0.0,"time":"18:07:00"},"-Mnbk6wd-wJze8P0JknK":{"milliliters":0.12,"time":"18:07:00"},"-Mnbk7Ek629vgBu-MiLg":{"milliliters":44.91,"time":"18:07:00"},"-Mnbk7bPuzqwsz9d5nm6":{"milliliters":5.43,"time":"18:07:00"},"-Mnbk7v7MADi7YzEbeFI":{"milliliters":24.54,"time":"18:07:00"},"-Mnbk8DGfqswckdsA1qP":{"milliliters":47.58,"time":"18:07:00"},"-Mnbk8Xw2kJPxLrqCl6h":{"milliliters":13.98,"time":"18:07:00"}}
-
-
Nairobi about 2 yearsThank you very much, that was in detail and It helped alot
-
msbit about 2 yearsThis is just plain wrong. All it does is change the error from a runtime error (as above) to a compile time error, as now the compiler knows the type is not an
Iterable
. Have you actually tried this out @Nairobi or @Canada2000? -
Nairobi about 2 yearsYeah, I did try it and I had to just alter the For Loop and it was able to fetch the JSON file from firebase
-
Nairobi about 2 years@msbit I also alterd my code as you indicated and it worked perfectly.