json in listview flutter 1

json in listview flutter

Future fetchPost() async {
  final response =
  await http.get('http://simone.fabriziolerose.it/index.php/Hello/dispdataflutter');

  if (response.statusCode == 200) {
    // If the call to the server was successful, parse the JSON.
    return Post.fromJson(json.decode(response.body));
  } else {
    // If that call was not successful, throw an error.
    throw Exception('Failed to load post');

class Post {
  final int id;
  final String title;
  final String body;

  Post({ this.id, this.title, this.body});

  factory Post.fromJson(Map json) {
    return Post(

      id: json['id'],
      title: json['testo'],
      body: json['stato'],

void main() => runApp(MyApp(post: fetchPost()));

class MyApp extends StatelessWidget {
  final Future post;

  MyApp({Key key, this.post}) : super(key: key);

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fetch Data Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      home: Scaffold(
        appBar: AppBar(
          title: Text('Fetch Data Example'),
        body: Center(
          child: FutureBuilder(
            future: post,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data.title);
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");

              // By default, show a loading spinner.
              return CircularProgressIndicator();

Here is what the above code is Doing:
1. The main() function uses runApp() to start the app.
2. The app’s main widget is a MaterialApp.
3. The MaterialApp has a Scaffold.
4. The Scaffold has a body that consists of a Center widget.
5. The Center widget has a FutureBuilder.
6. The FutureBuilder has a future that returns a Post.
7. The FutureBuilder has a builder that returns a Text widget.
8. The Text widget displays the title of the post.

Similar Posts