数据不会返回到颤振中的未来构建

时间:2021-07-12 09:50:12

标签: flutter api dart listview flutter-futurebuilder

...
Future<List<Category>> fetchCategorys(id) async {
  var body = jsonEncode({"service_id": id});
  print("service_id : " + id);
  http.Response response2 = await http.post(
      Uri.encodeFull(api + "get_order_product_details.php"), //url
      headers: {"Accept": "application/json"},
      body: body);
  if (response2.statusCode == 200) {
    // Map<String, dynamic> map = json.decode(response2.body);
    // var map = json.decode(response.body);
    // print("map : " + "${response.body}");
    // print("map : " + "${map['Data']}");
    List<Category> albumList;
    albumList = (json.decode(response2.body) as List)
        .map((i) => Category.fromJson(i))
        .toList()`enter code here`;
    return albumList;
  } else {
    // print("Failed to load categories");
    throw Exception('Failed to load category products');
  }
}
...

在此,返回的专辑列表不会出现在未来构建器中的快照

...
 child: FutureBuilder<List<Category>>(
          future: fetchCategorys(id),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            print("snapshot data : " + "${snapshot.data}");
            if (snapshot.data == null) {
              return Container(
                  child: Center(
                child: Text("Loading..."),
              ));
            } else {
...

这里的打印结果是'I/flutter (6977): snapshot data : null'

1 个答案:

答案 0 :(得分:0)

问题可能是未来会抛出错误,您也应该检查未来构建器上的错误状态。查看官方文档以了解如何查看所有状态 https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html#widgets.FutureBuilder.1