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);
Share:
802
Nairobi
Author by

Nairobi

Updated on December 06, 2022

Comments

  • Nairobi
    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
      Frank van Puffelen about 2 years
      What does getJsonFromFirebase look like?
    • Nairobi
      Nairobi about 2 years
      Hi 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
      Nairobi about 2 years
      When you click the link the JSON file looks like this {"-Mnbk2ye2P8bfpaQvNaU":{"milliliters":0.0,"time":"18:07:00"‌​},"-Mnbk6wd-wJze8P0J‌​knK":{"milliliters":‌​0.12,"time":"18:07:0‌​0"},"-Mnbk7Ek629vgBu‌​-MiLg":{"milliliters‌​":44.91,"time":"18:0‌​7:00"},"-Mnbk7bPuzqw‌​sz9d5nm6":{"millilit‌​ers":5.43,"time":"18‌​:07:00"},"-Mnbk7v7MA‌​Di7YzEbeFI":{"millil‌​iters":24.54,"time":‌​"18:07:00"},"-Mnbk8D‌​GfqswckdsA1qP":{"mil‌​liliters":47.58,"tim‌​e":"18:07:00"},"-Mnb‌​k8Xw2kJPxLrqCl6h":{"‌​milliliters":13.98,"‌​time":"18:07:00"}}
  • Nairobi
    Nairobi about 2 years
    Thank you very much, that was in detail and It helped alot
  • msbit
    msbit about 2 years
    This 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
    Nairobi about 2 years
    Yeah, 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
    Nairobi about 2 years
    @msbit I also alterd my code as you indicated and it worked perfectly.