0

I am new to flutter and want to parse the data from a URL that is in json format. The url that I am using is json link . I want to get the "verse" and "chapter" fields from the json array. I have succeeded in getting the response body in snackbar but not able to get single values. I want to get the "verse" and "chapter" and show then in text box. I am using Dart.

Here is the method that I am using:

 _makeGetRequest() async {
    // make request
    var url = Uri.parse("http://quotes.rest/bible/vod.json");
    Response response = await http.get(url);

    // sample info available in response
    int statusCode = response.statusCode;
    Map<String, String> headers = response.headers;
    String contentType = headers['content-type'];
    String json = response.body;

    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(json)));

  }

 @override
  void initState() {
    super.initState();
    streamingController.config(url: "http://stream.zeno.fm/qa8p6uz2tk8uv");
    streamingController.play();
    _makeGetRequest();
  }

Please help me in getting the proper field values and set them in text box as I am trying to solve it from past two days and have tried all solutions from internet but getting exceptions.

2

1 Answer 1

6

use jsonDecode

 Map<String,dynamic> data = jsonDecode(response.body);
 String verse = data["contents"]["verse"];
 dynamic chapter= data["contents"]["chapter"];

however I recommend modeling your data

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

5 Comments

Thank u it worked for me.. I am able to show it in snackbar .. But How can I show it in the text box as this is inside a method?
and if previous answer solved your problem upvote and mark question as answered ;)
How can I return the verse string so it will be accessible in class?
Future<String> _makeGetRequest() async { await someApiWork(); return verse; }

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.