颤振http获取json数据

颤振http获取json数据,json,flutter,http,dart,Json,Flutter,Http,Dart,我在获取数据方面有困难。我是一个新手,虽然我读了这些文章,但我无法克服这个问题。我能够单独获取数据,但无法将其放入循环中。代码如下 Future<List<Post>> getPosts() async { var jsonData = await http.get("https://example.com/api/posts.php"); final jsonResponse = json.decode(jsonData.body);

我在获取数据方面有困难。我是一个新手,虽然我读了这些文章,但我无法克服这个问题。我能够单独获取数据,但无法将其放入循环中。代码如下

    Future<List<Post>> getPosts() async {
  var jsonData = await http.get("https://example.com/api/posts.php");
  final jsonResponse = json.decode(jsonData.body);
  Match postList= Post.fromJsonMap(jsonResponse);
  return (jsonResponse as List)
      .map((postList) => Post.fromJsonMap(postList))
      .toList();
  //print("post" + postList.result[1].home);
}
Future getPosts()异步{
var jsonData=等待http.get(“https://example.com/api/posts.php");
final jsonResponse=json.decode(jsonData.body);
Match postList=Post.fromJsonMap(jsonResponse);
返回(jsonResponse作为列表)
.map((postList)=>Post.fromJsonMap(postList))
.toList();
//打印(“post”+postList.result[1].home);
}
当我运行print方法时,我可以打印数据。然而,当我将其发送给futurebuilder时,数据并没有出现

body: FutureBuilder(
          future: getPosts(),
          builder: (BuildContext context, AsyncSnapshot<List<Post>> snapshot) {
            if (snapshot.hasData) {
              print("test");
              return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(snapshot.data[index].result[index].home),
                      subtitle: Text(snapshot.data[index].result[index].title),
                      leading: CircleAvatar(
                        child: Text(
                            snapshot.data[index].result[index].id.toString()),
                      ),
                    );
                  });
            } else {
              return Center(child: CircularProgressIndicator());
            }
          }),
body:FutureBuilder(
future:getPosts(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasData){
打印(“测试”);
返回ListView.builder(
itemCount:snapshot.data.length,
itemBuilder:(上下文,索引){
返回列表块(
标题:文本(快照.数据[索引].结果[索引].主),
字幕:文本(快照.数据[索引].结果[索引].标题),
领先:CircleAvatar(
子:文本(
snapshot.data[index].result[index].id.toString()),
),
);
});
}否则{
返回中心(子项:CircularProgressIndicator());
}
}),

注意:我用作示例的代码:

尝试添加额外的if分支,如下所示

} else if (snapshot.hasError) {
    print(snapshot.error);
} else {

尝试添加额外的if分支,如下所示

} else if (snapshot.hasError) {
    print(snapshot.error);
} else {

您可以用于解析数据并将其转换为您的模型

您可以用于解析数据并将其转换为您的模型

我使用Json创建了我的模型,但没有数据。JSON数据有问题吗?我可以从另一个来源提取数据。(jsonplaceholder)我使用的JSON类型是:{“success”:true,“result”:[{“id”:“3970”,“home”:“Test”,“title”:“TestTitle”}],“premium”:true,“type”:null}使用jsonDecode(stringData);并将其传递给model.fromJson();我使用Json创建了我的模型,但没有数据。JSON数据有问题吗?我可以从另一个来源提取数据。(jsonplaceholder)我使用的JSON类型是:{“success”:true,“result”:[{“id”:“3970”,“home”:“Test”,“title”:“TestTitle”}],“premium”:true,“type”:null}使用jsonDecode(stringData);并将其传递给model.fromJson();生成FutureBuilder时抛出了以下断言(脏,状态:_FutureBuilderState#a36f2):生成函数返回null。生成FutureBuilder时抛出了以下断言(脏,状态:_FutureBuilderState#a36f2):生成函数返回null。您可以用
打印(snapshot.data)
代替
打印(“测试”)
让我知道你得到了什么?你能用这个
打印(snapshot.data)
代替
打印(“测试”)
让我知道你得到了什么吗?