Flutter api数据未显示Futurebuilder小部件

时间:2020-10-28 13:01:13

标签: flutter

我正在从api中获取数据,并仅在Futurebuilder小部件中打印快照数据,但它向我显示了此错误

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'

这就是我调用API的方式

 Future<List> dosomestuff() async {

      http.Response res = await http.get(
        'http://api-uat.thelunchbox-restaurant.com/api/orders/admin/4',
      );
      var data = json.decode(res.body);
      print(data);
      print(data["Orders"]);
      return data;
  }

我未来的建造者代码

FutureBuilder(
                      future: dosomestuff(),
                      builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
                        if (snapshot.connectionState == ConnectionState.done) {
                          if (snapshot.hasData) {
                            print('working');
                            print(snapshot);
                            return Center(
                              child: Text("done ...!!"),
                            );
                          }
                        }
                        return Center(
                          child: Text("Loading ...!!"),
                        );
                      })

在这样的未来功能中打印数据

I/flutter (16288): {Orders: [{OrderID: 208, CustomerID: 2, TransactionNo: 80, OrderNo: 1, OrderType: 1, OrderDate: 2020-10-28T13:13:44.99, OrderPreparedDate: null, OrderOFDDate: null, OrderDoneDate: null, StatusID: 101, SessionID: POS-KXCBSH636794256705513894, OrderTakerID: null, DeliverUserID: null, LastUpdateBy: 2, LastUpdateDT: 2020-10-28T13:13:44.99, LocationID: 4, BrandID: 1, BrandName: The LunchBox, BrandLogo: http://dashboard-uat.thelunchbox-restaurant.com/assets/Upload/Brand/b8642f71-685a-4ff3-9045-abcbc5cd01ea.jpg, Remarks: null, OrderCheckouts: {OrderCheckoutID: 207, OrderID: 208, PaymentMode: 1, AmountPaid: 17.3, AmountTotal: 17.3, ServiceCharges: 0.0, GrandTotal: 2.0, Tax: 0.3, CheckoutDate: 2020-10-28T13:13:42.96, SessionID: null, StatusID: 101, LastUpdateBy: null, LastUpdatedDate: null}, CustomerOrders: {CustomerOrderID: 205, Name: null, Email: rafi@garage.sa, Mobile: 0544916463, Description: , Address: Paris, France, Longitude: 46.6753, Latitude: 24.7136, LocationURL: null, StatusID: 1, LastUpdatedBy: 2, LastUpd

并且它没有显示完整,我认为这是以字符串或其他形式显示。我该如何解决futurebuilder中的错误?

1 个答案:

答案 0 :(得分:1)

问题是您的数据仍然是String格式,不在List中,而在Future Widget中,其定义为列表。 因此,您需要在列表中转换API响应。显示数据打印时,您可以这样做

  Future<List> dosomestuff() async {

      http.Response res = await http.get(
        'http://api-uat.thelunchbox-restaurant.com/api/orders/admin/4',
      );

      Map<String, dynamic> map = json.decode(res.body);
      List<dynamic> data = map["Orders"];
      print(data);
      return data;
  }