簡體   English   中英

在顫振/飛鏢中的 api 調用中獲取數據的問題

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

如何獲取此 API 數據中的nameteam_name鍵? 條件:這里的 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
}

使用此代碼。 您可以檢查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