我有这个变量问题,这是我的getdata
函数
Future <List <Deal>> getData() async{
String myUrl = "http://10.25.20.27:5000/api/all";
var response = await http.get(myUrl,
headers: {
'Accept':'application/json',
});
var jsonData = json.decode(response.body);
List<Deal> deals =[];
var u;
for( u in jsonData){
Deal deal = Deal(u["id"],u["name"],u["adress"],u["photo"],u["Description"],u["discount"]);
}
return deals;
}
我得到了这个错误
进行热重装.在902ms中重新加载594个库中的14个。E/ DatabaseHelper.getData ( 4211):ERROR:DatabaseHelper.getData/lib/ui/ui_dart_state.cc(148)未处理的异常:类型'String‘不是'index’E/ _HomeePageState.build的'int‘类型的子类型( 4211):#0 DatabaseHelper.getData_HomeePageState.build E/_HomeePageState.build( 4211):e/_HomeePageState.build( 4211):#1 _HomeePageState.build。(包装:_InkResponseState._handleTap _app/homee_Page.DAT:100:80)E/_InkResponseState._handleTap( 4211):#2 _InkResponseState._handleTap _InkResponseState.build E/颤振( 4211):#3 _InkResponseState.build。(package:flutter/src/material/ink_well.dart:729:32) E/颤振( 4211):#4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) E/颤振( 4211):#5 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11) E/颤振( 4211):#6 TapGestureRecognizer.handlePrimaryPointer (包装:颤振/src/手势/踢踏舞:275:7)E/ PrimaryPointerGestureRecognizer.handleEvent ( 4211):#7 PrimaryPointerGestureRecognizer.handleEvent
发布于 2019-11-12 12:52:45
我认为你可以创建如下“交易”类:-
class Deal {
String id;
String name;
String adress;
String photo;
String Description;
String discount;
Deal(
this.id,
this.name,
this.adress,
this.photo,
this.Description,
this.discount,
);
}
然后您可以使用如下循环:-
var u;
for (u in jsonData) {
Deal deal = Deal(
u["id"].toString(),
u["name"].toString(),
u["adress"].toString(),
u["photo"].toString(),
u["Description"].toString(),
u["discount"].toString());
}
https://stackoverflow.com/questions/58815491
复制相似问题