首页 > 解决方案 > Flutter api 数据未显示 Futurebuilder 小部件

问题描述

我正在从 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 ...!!"),
                        );
                      })

它在 Future 函数中的打印数据是这样的

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中的错误?

标签: flutter

解决方案


Issue is your data is still in String format not in List and in Future Widget its define as a list. So you need to convert your API response in a list. As you show your data print you can do like this

  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;
  }

推荐阅读