0

Ive been trying to fetch json data from my flutter app. I managed to print the whole json on my console. I want to get only the "score" value from this json. How should I do it?

  final String url = "http://www.mocky.io/v2/5aa3f9ee310000c21926e2f8";

  Future<String> getJsonData() async{
    http.Response response = await http.get(
      Uri.encodeFull(url),
      headers: {"Accept" : "application/json"}
    );
     print(response.body);

    List data = json.decode(response.body);
    print(data[0]["score"]);

  }

1 Answer 1

2

Update your last two lines as

 Map<String, dynamic> data = json.decode(response.body);
 print(data["score"]);

Explanation: Data you receive from API is not JSON Array its JSON object so you should decode response to Map, & get score field from that map.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.