【问题标题】:error : Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'错误:未处理的异常:类型'String'不是'index'类型'int'的子类型
【发布时间】:2020-06-05 22:47:37
【问题描述】:

我的应用程序标题中有错误。 它说它来自那里:

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;

}

"Jack" est une classe définie ci - dessous :

class Jack {

  final String j7;
  final String j14;

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

jsondata2中的api返回这个结果:

[{j14:0, j7:0}]

你有没有看到任何错误???

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您的 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;
    
    }
    

    【讨论】:

      【解决方案2】:

      改变

      字符串jackpot7; 字符串jackpot14;

      int jackpot7; int jackpot14;

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2021-07-08
          • 2020-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-22
          • 1970-01-01
          • 2021-09-19
          相关资源
          最近更新 更多