我有这个变量问题
这是我的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;
}
我收到此错误
执行热重装... 902毫秒内重新加载594个库中的14个。 E / flutter(4211):[错误:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:类型'String'不是'index'的'int'类型的子类型 E / flutter(4211):#0 DatabaseHelper.getData(package:flutter_app / databasehelper.dart:116:25) E /颤振(4211): E / flutter(4211):#1 _HomeePageState.build。 (package:flutter_app / homee_page.dart:100:80) E / flutter(4211):#2 _InkResponseState._handleTap(包:flutter / src / material / ink_well.dart:654:14) E / flutter(4211):#3 _InkResponseState.build。 (包:flutter / src / material / ink_well.dart:729:32) E / flutter(4211):#4 GestureRecognizer.invokeCallback(package:flutter / src / gestures / recognizer.dart:182:24) E / flutter(4211):#5 TapGestureRecognizer._checkUp(包:flutter / src / gestures / tap.dart:365:11) E / flutter(4211):#6 TapGestureRecognizer.handlePrimaryPointer(包:flutter / src / gestures / tap.dart:275:7) E / flutter(4211):#7 PrimaryPointerGestureRecognizer.handleEvent(package:flutter / src / gestures / recognizer.dart:455:9)
答案 0 :(得分:0)
我认为您可以创建“交易”类,如下所示:-
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());
}