错误:未处理的异常:类型'String'不是'index'的'int'类型的子类型

时间:2020-06-05 22:47:37

标签: flutter

我的应用标题出现错误。 它说它是从那里来的:

Future <Jack> Get_Amount_Jackpot() async {
// SERVER LOGIN API URL
var url2 = 'https://www.easytrafic.fr/game_app/get_jackpot_lottosport.php';

// Starting Web API Call.
var response2 = await http.get(url2,headers: {'content-type': 'application/json','accept': 'application/json','authorization': globals.token});

// Getting Server response into variable.
Jack jackpots;

var jsondata2 = json.decode(response2.body);

String jackpot7;
String jackpot14;

for (var u in jsondata2) {
  jackpot7=u["j7"];
  jackpot14=u["j14"];
}

jackpots=Jack(jackpot7, jackpot14);

return jackpots;

}

“杰克”和“définieci”类-dessous:

class Jack {

  final String j7;
  final String j14;

  const Jack(this.j7,this.j14);
}

api在jsondata2中返回以下结果:

[{j14:0,j7:0}]

您看到任何错误了吗?

3 个答案:

答案 0 :(得分:0)

您的API结果[{j14: 0, j7: 0}]包含int值,但是您的模型具有String

您的模特应该是

class Jack {

  final int j7;
  final int j14;

  const Jack(this.j7,this.j14);
}

并且您的api函数应该是

Future <Jack> Get_Amount_Jackpot() async {
   // SERVER LOGIN API URL
   var url2 = 'https://www.easytrafic.fr/game_app/get_jackpot_lottosport.php';

   // Starting Web API Call.
   var response2 = await http.get(url2,headers: {'content-type': 'application/json','accept': 'application/json','authorization': globals.token});

   // Getting Server response into variable.
   Jack jackpots;

   var jsondata2 = json.decode(response2.body);

   String jackpot7;
   String jackpot14;

   for (var u in jsondata2) {
     jackpot7=u["j7"].toString();
     jackpot14=u["j14"].toString();
   }

   jackpots=Jack(jackpot7, jackpot14);

   return jackpots;

}

答案 1 :(得分:0)

更改

String jackpot7; 字符串jackpot14;

int jackpot7; int jackpot14;

答案 2 :(得分:0)

我刚刚在float中更改了数据api类型,现在可以使用