10 Flutter: ListView with JSON or List Data

main.dart
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(new MaterialApp(
home: new HomePage()
));
}
class HomePage extends StatefulWidget {
HomePageState createState() => new HomePageState();
}
class HomePageState extends State<HomePage> {
List data;
Future<String> getData() async {
var response = await http.get(
Uri.encodeFull("https://jsonplaceholder.typicode.com/posts"),
headers: {
"Accept": "application/json"
}
);
this.setState(() {
data = JSON.decode(response.body);
});
print(data[1]["title"]);
return "Success!";
}
void initState(){
this.getData();
}
Widget build(BuildContext context){
return new Scaffold(
appBar: new AppBar(title: new Text("Listviews"), backgroundColor: Colors.blue),
body: new ListView.builder(
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index){
return new Card(
child: new Text(data[index]["title"]),
);
},
),
);
}
}