flutter - 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中的错误?
解决方案
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;
}
推荐阅读
- java - TextFieldWidget 不适用于 FabricMC(Java)
- c++ - 如何在模板特化中使用私有内部模板类作为参数?
- unit-testing - 每次测试笑话后清除模拟
- audio - 使用 ffmpeg 解码时理解音频流样本数的问题
- php - 如何在 PHP/SQL/AJAX 中获取数据的最后递增值
- python - 模块“sample_project”没有属性“data”
- r - R: if ... { 中的错误:需要 TRUE/FALSE 的缺失值
- python - 使用 Pylint 时,如何使用 pyproject.toml 设置文件声明有效名称的特定变量列表
- angular - Angular 8+ 中的 GSAP 时间线
- r - 用 R 模拟