简体   繁体   English

在颤振/飞镖中的 api 调用中获取数据的问题

[英]Problem to fetch data in api call in flutter/dart

how can I fetch the name and team_name keys in this API data?如何获取此 API 数据中的nameteam_name键? condition: here 18,1,17, etc are subject codes that change according to the subject and not fix this subject available in the next API call.条件:这里的 18、1、17 等是根据主题更改的主题代码,并且不会在下一个 API 调用中修复此主题。

{
    "18": {
        "detail": {
            "id": "18",
            "name": "Hindi"
        },
        "list": [
            {
                "id": "5",
                "team_name": "Gurpreet",
            },
            {
                "id": "2",
                "team_name": "Test1",
            }
        ]
    },
    "17": {
        "detail": {
            "id": "17",
            "name": "Punjabi"
        },
        "list": [
            {
                "id": "6",
                "team_name": "Guru",
            },
            {
                "id": "3",
                "team_name": "Test",
            }
        ]
    },
    "1": {
        "detail": {
            "id": "1",
            "name": "History"
        },
        "list": [
            {
                "id": "7",
                "team_name": "Gurpreet",
            }
        ]
    },
    "19": {
        "detail": {
            "id": "19",
            "name": "Math"
        },
        "list": [
            {
                "id": "4",
                "team_name": "Gurpreet",
            }
        ]
    },
    "status": true
}

Use this code.使用此代码。 You can check keys getter to check dynamics key.您可以检查keys获取器以检查动态键。

import 'dart:convert';

void main() async {
  var f = {
    "18": {
      "detail": {"id": "18", "name": "Hindi"},
      "list": [
        {
          "id": "5",
          "team_name": "Gurpreet",
        },
        {
          "id": "2",
          "team_name": "Test1",
        }
      ]
    },
    "17": {
      "detail": {"id": "17", "name": "Punjabi"},
      "list": [
        {
          "id": "6",
          "team_name": "Guru",
        },
        {
          "id": "3",
          "team_name": "Test",
        }
      ]
    },
    "1": {
      "detail": {"id": "1", "name": "History"},
      "list": [
        {
          "id": "7",
          "team_name": "Gurpreet",
        }
      ]
    },
    "19": {
      "detail": {"id": "19", "name": "Math"},
      "list": [
        {
          "id": "4",
          "team_name": "Gurpreet",
        }
      ]
    },
    "status": true
  };

  for (var o in f.keys) {
    print(o);
    if (f[o] is bool) {
      print(f[o]);
    } else { // check it is Map. I consider it always is Map
      if ((f[o] as Map)['detail'] != null) {
        print((f[o] as Map)['detail']['name']);
      }
      if ((f[o] as Map)['list'] != null) {
        print((f[o] as Map)['list'][0]['team_name']); // you can use for here. please check array is not null
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM