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)
},
),
),
);
}
}
CodePudding user response:
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]);
}
},
),