代码之家  ›  专栏  ›  技术社区  ›  Darshan

如何将变量(&id)从一个api传递到另一个api以获取相应的数据?

  •  0
  • Darshan  · 技术社区  · 7 年前

    我想在游戏中显示玩家的统计数据 listview 为此,我正在使用此api: https://cricapi.com/api/playerStats?apikey=apikey&pid=pid

    上述api的输出为:

    {
      "pid": xxxx,
      "profile": "profile description",
      "imageURL": "https://www.cricapi.com/playerpic/xxxx.jpg",
    

    pid

    https://cricapi.com/api/playerFinder?apikey=apikey&name=playerName

    上述api的输出为:

    {
      "data": [
        {
          "pid": xxxx,
          "fullName": "Firstname Lastname",
    

    pid 在第一个显示玩家统计信息和代码的api中:

    FetchJson() async {
    var response = await http.get(
        'https://cricapi.com/api/playerStats?apikey=apikey&pid=1111');
    
    if (response.statusCode == 200) {
      String responseBody = response.body;
      var responseJson = jsonDecode(responseBody);
      pid = responseJson['pid'];
      name = responseJson['name'];
      playingRole = responseJson['playingRole'];
      battingStyle = responseJson['battingStyle'];
      country = responseJson['country'];
      imageURL = responseJson['imageURL'];
      data = responseJson;
    
      var stats = data['data']['batting'];
      var testStats = stats['tests'];
      var odiStats = stats['ODIs'];
      var tStats = stats['T20Is'];
    
      // T20 Stats
    
      matches_t = tStats['Mat'];
      runs_t = tStats['Runs'];
      half_t = tStats['50'];
      century_t = tStats['100'];
      highest_t = tStats['HS'];
      avg_t = tStats['Ave'];
    

    我正在打电话 FetchJson() 在…内 initState() .

    我试着回答我的类似/先前的问题 How to fetch api data by passing variables (parameters)? ,但这让我走上了另一条路。我无法实现这个解决方案,因为我无法返回 pid 通过将由接收的第一个api FetchJson() .

    我的问题是:

    pid pid ,我可以通过 pid 作为变量,可以在UI中显示多个玩家的统计信息?

    所需代码如下: https://pastebin.com/iU8x9U8z

    playerid 这是显示我只有一个球员的统计数据,但我想显示不同的球员统计数据。

    **********更新*************

    作为替代解决方案,我现在使用 pids 并使用 map FetchJson() 内部for循环,如下所示:

    var playerIds = [{"pid":35320},{"pid":28114},{"pid":28779},{"pid":28763},{"pid":30176},{"pid":7133},{"pid":5390}]
    
    @override
      void initState() {
        var intIds = playerIds.map<int>((m) => m['pid'] as int).toList();
        for (int i = 0; i < intIds.length; i++) {
          FetchJson(intIds[i]);
        }
      }
    
    FetchJson(int ids) async {
        print(ids);
        var response = await http.get(
            'https://cricapi.com/api/playerStats?apikey=apikey&pid=$ids');
    ....
    }
    

    我现在面临的问题是,这种方法的最后一步 pid 并在UI中重复显示其数据。我希望看到的预期输出是:所有玩家的数据 皮德斯 在UI中,我不确定如何实现这一点。

    在此处填写参考代码: https://pastebin.com/kFYBfHuf

    1 回复  |  直到 7 年前
        1
  •  1
  •   Charles Jr    7 年前

    Maps where 子句以标识匹配的数据。

    最大的问题是您需要在两个api中识别匹配的数据项。在我的示例中,我假设它可能是一个球员的名字,也可能是他们的球队和球队编号,但必须有某种东西来验证您正在查看同一球员的不同数据点。

    switch(variable_expression) { 
       case name = full_name: { 
          // statements; 
       } 
       break; 
    
       case constant_expr2: { 
          //statements; 
       } 
       break; 
    
       default: { 
          //statements;  
       }
       break; 
    }