Issue
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'
Solution
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);
Answered By - Canada2000
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.