how to load Json to flutter listview

6,254

I'd rather return List of Post from fetchPost(), like below :

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

  if (response.statusCode == 200) {
  List responseList = json.decode(response.body);
  return responseList.map((data)=>Post.fromJson(data)).toList();
  } else {
  // If that call was not successful, throw an error.
  throw Exception('Failed to load post');
  }
}

and use ListView.builder instead like this

List<Post> post;

child: ListView.builder(
  itemCount: post.length,
  physics: BouncingScrollPhysics(),
  padding: EdgeInsets.all(0),
  itemBuilder: (context, index){
    return ListAdapter(
       id: post[index].id,
       title: post[index].title,
       body: post[index].body
       );
    },
  )
Share:
6,254
Marie Hopkin
Author by

Marie Hopkin

Updated on December 12, 2022

Comments

  • Marie Hopkin
    Marie Hopkin over 1 year

    I'm new to flutter and I'm developing a flutter app where I have to show in a listView the data on the database.

    I get the data in JSON format.

    {
        "data": [
            {
                "id": “1”,
                "testo": "Fare la spesa",
                "stato": "1"
            },
            {
                "id": “2”,
                "testo": "Portare fuori il cane",
                "stato": "1"
            },
            {
                "id": “3”,
                "testo": “jhon”,
                "stato": "0"
            }
        ]
    }
    

    The problem is that I don't load the data, I can't understand how to do it.

    I should read 'Data' with an array but I don't know how to do it.

    Can anyone help me figure out how to do it?

    Thanks for your help.

    PS. I tried this tutorial

    Main.dart

    Future<Post> 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<String, dynamic> 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> post;
    
      MyApp({Key key, this.post}) : super(key: key);
    
      @override
      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<Post>(
                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();
                },
              ),
            ),
          ),
        );
      }
    }