【问题标题】:Flutter api data not showing Futurebuilder widgetFlutter api 数据未显示 Futurebuilder 小部件
【发布时间】:2021-02-10 20:25:22
【问题描述】:

我正在从 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


    【解决方案1】:

    问题是您的数据仍然是字符串格式而不是列表,并且在 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;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 2021-01-02
      • 2019-08-14
      • 2023-04-10
      • 2020-08-25
      • 2022-01-02
      • 2021-03-04
      相关资源
      最近更新 更多