简体   繁体   English

Flutter:如何将来自 api 响应的特定数据存储在列表中

[英]Flutter: How to store specifc data from api response in a list

I have an api which returns json object as response.我有一个 api 返回 json object 作为响应。 I have to store all the 'prefix_number' from response into list to later use it in a dropdown.我必须将响应中的所有“prefix_number”存储到列表中,以便以后在下拉列表中使用它。 I am trying to do it but no success.我正在尝试这样做,但没有成功。

here is response from api这是来自 api 的回复

[{prefix_number: 50}, {prefix_number: 51}, {prefix_number: 52}, {prefix_number: 53}, {prefix_number: 54}, {prefix_number: 55}, {prefix_number: 56}, {prefix_number: 57}, {prefix_number: 58}]

Here is my a list where i want to store it这是我要存储的列表

List<String> prefixList = [];

here is my method这是我的方法

 Future getNumberPrefixes() async {
    var url = Uri.parse(AppApis.getPrefixesApi);
    final response = await http.get(url);
    print(response.statusCode);
    if (response.statusCode == 200) {
      Map data = jsonDecode(response.body);
      print(data);
      // print(data['data'][0]['prefix_number']);
      notifyListeners();
    } else {
      print("Failed");
    }
  }

Please help请帮忙

Give this a go:给这个 go:

  final data = jsonDecode(payload) as List<dynamic>;
  final prefixList = data.map((d) => (d as Map<String, dynamic>)["prefix_number"].toString()).toList();
  print(prefixList);

Here is a working example: https://dartpad.dev/?id=26e63a1baa818af7f3bea41a57eb1556这是一个工作示例: https://dartpad.dev/?id=26e63a1baa818af7f3bea41a57eb1556

Here is my code for you.这是我给你的代码。

Future getNumberPrefixes() async {
    var url = Uri.parse(AppApis.getPrefixesApi);
    final response = await http.get(url);
    print(response.statusCode);
    if (response.statusCode == 200) {
      Map data = jsonDecode(response.body);
     
     //ADD Following lines to your code.

      for(int i=0; i< data.length; i++){
        prefixList.add(data[I]["prefix_number"].toString());
      }


      print(data);
      notifyListeners();
    } else {
      print("Failed");
    }
  }

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

相关问题 如何将 api 响应列表的每个元素存储到列表<string>在 flutter?</string> - how to store every element of api response list to a List<String> in flutter? 如何将来自 api 的复杂 json 响应存储在 flutter 的本地数据库中? - How to store a complex json response from api in local database in flutter? 如何从 API 中获取数据并存储到 Flutter 中的变量中? - How to fetch the data from the API and store into a variable in flutter? Flutter:如何从响应 API 显示数据 HTML 结果? - Flutter : How to show data HTML result from response API? 如何将您的 api 响应保存为 flutter 中的列表 - How to save your api response in as a list in flutter 如何序列化 api 响应包含 flutter 中的列表? - How to serialize api response contains list in flutter? 如何从 flutter 可搜索下拉列表中的 api 列表中搜索数据 - How to Search data from api list in flutter searchable dropdown list 使用 flutter 在 firebase collections 中存储一个 api 响应数据 - Store a api response data in firebase collections using flutter 如何在 flutter 中返回 api 响应值列表 - how to return list of api response values in flutter 如何以及在何处存储 api 响应数据,以便在 flutter 中需要时可以使用我? - How and where to store api response data so that i can be used whenever needed in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM