快照数据始终为空 Future

时间:2021-05-14 18:51:19

标签: flutter listview

我正在尝试集成 api 以获取列表,代码适用于其他 Api 但对于此快照始终保持为空。 我不知道为什么它向我显示错误。

Future<List<Allcars>> _fetchVehicle() async {
// your url with parameters
var uri = new Uri.http(url, "/API/SearchCarSale/Get", {
  "Pricemin": widget.carMinPrice.split('.')[0].toString(),
  "PriceMax": widget.carMaxPrice.split('.')[0].toString(),
  // "Pricemin": '0',
  // "PriceMax": '10000',
  "Model": widget.carModel, //camry
  "ManufacturerName": widget.carManuacturer, //toyata
  "Category": widget.carCategory, //SUV
  "Year": widget.carYear, //2015
});
print(uri);
final response = await http.get(uri);
print(response.statusCode);
if (response.statusCode == 200) {
  List jsonResponse = json.decode(response.body);
  print(json.decode(response.body));
  return jsonResponse.map((car) => new Allcars.fromJson(car)).toList();
} else {
  throw Exception('Error');
}

}

主要功能是

 @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('All Vehicles'),
        ),
        body: FutureBuilder<List<Allcars>>(
          future: _fetchVehicle(),
          builder: (context, AsyncSnapshot snapshot) {
            if (snapshot.hasData) {
              print('snap data');
              List<Allcars> data = snapshot.data;
              // return _vehiclesListView(data);
              //
                return  new ListView.builder(
                itemCount: data == null ? 0 : data.length,
                itemBuilder: (BuildContext context, int index) {
                  return new Card(
                    child: new Text(data[index].carPrice),
                    // new
                    //  Text(data[index].carPrice),
                  );
                },
              );
              //
            } else if (snapshot.hasError) {
              return Center(
                child: Text(
                    'Failed to load data from internet. ${snapshot.hasError}'),
              );
            }
            return Center(
              child: Text("Loading..."),
            );
          },
        ));
  }

未填充列表视图。快照的数据总是为空,我该怎么办?

0 个答案:

没有答案