当前位置: 首页 > 知识库问答 >
问题:

未处理的异常:类型'String'不是'index'问题Dart的类型'int'的子类型

萧元徽
2023-03-14

我有这个变量问题,这是我的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/flatter(4211):[错误:flatter/lib/ui/ui#dart_state.cc(148)]未处理的异常:类型“String”不是“index”E/flatter(4211)的类型“int”的子类型:\0 DatabaseHelper。getData(包:flatter_app/databasehelper.dart:116:25)E/flatter(4211):E/flatter(4211):#1 40; HomeePageState。建筑(软件包:flatter_app/homee_page.省道:100:80)E/flatter(4211):#2!_把手(包装:颤振/src/材料/墨水井。省道:654:14)E/颤振(4211):#3#。建筑(包装:flift/src/material/ink_well.省道:729:32)E/flift(4211):#4手势识别器。调用Callback(包:flatter/src/signatures/recognizer.dart:182:24)E/flatter(4211):#5个TapGestureCongnizer_检查(软件包:flift/src/signatures/tap.dart:365:11)E/flift(4211):#6 TapGestureRecognizer。handlePrimaryPointer(软件包:flatter/src/signatures/tap.dart:275:7)E/flatter(4211):#7 PrimaryPointer检测识别器。handleEvent(软件包:颤振/src/手势/识别器。dart:455:9)

共有1个答案

斜高翰
2023-03-14

我认为您可以创建如下“交易”类:-

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());
}
 类似资料: