Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get data from json based on index number in flutter

I have a json file from where I am collecting only email addresses, and want to print index number base output like 3rd record’s email address to Text widget..

class _HomeScreenState extends State<HomeScreen> {
  List<String> list = [];

  Future<List<String>> getcomments() async {
    Uri url =
        Uri.parse('https://jsonplaceholder.typicode.com/posts/1/comments');
    var response = await http.get(url);
    if (response.statusCode == 200) {
      var jsondata = json.decode(response.body);

      list.clear();
      for (var jdata in jsondata) {
        list.add(jdata['email']);
      }
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('JSON'),
      ),
      body: Center(
        child: FutureBuilder(
          future: getcomments(),
          builder: (context, snapshot) {
            return Text(snapshot.data[2].toString());
            //here i want only 3rd data(index 2)
          },
        ),
      ),
    );
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can you try this?

FutureBuilder(
      future: getcomments(),
      builder: (context, snapshot) {
        List<String> data = snapshot.data as List<String>;
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const CircularProgressIndicator();
        }
        else {
          return Text(data[2]);
        }
      },
    ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading