dart 如何在flutter apis中创建数组

7kqas0il  于 2023-02-20  发布在  Flutter
关注(0)|答案(2)|浏览(130)

我想提交选定的答案阵列怎么能做到这一点。
如何传递此数组中的选定答案
第一节第一节第一节第一节第二节第一节第三节第一节

mwg9r5ms

mwg9r5ms1#

Flutter/Dart没有数组。请改用列表。

uqzxnwby

uqzxnwby2#

溶液

在api-response中,它总是被格式化为List of MapMap of List
Map列表如下:

[

 {
   "name":"ruby",
   "gender":"female"
 },

 {
   "name":"otis",
   "gender":"male"
 },

]

而且,列表的Map类似于:

{
    "data": [
        "Professor",
        "Berlin",
        "Tokyo"
    ]
}

因此,要访问它们,您必须使用JsonDecode,然后查看响应并进行处理。
对于Map响应....

var resMap = jsonDecode(response.body);

对于列表响应(在resMap之后使用密钥,密钥带有密钥)...

var resMap = jsonDecode(response.body);
var infoList = resMap["data"];

演示

class CatTypeController {
  Future<void> getLeaves() async {
    String url = "https://meowfacts.herokuapp.com/?count=2";

    http.Response response = await http.get(Uri.parse(url));

    if (response.statusCode == 200) {
      Map rM = jsonDecode(response.body);
      print(rM["data"]);
    }
  }
}

响应

输出

["In ancient Egypt, killing a cat was a crime punishable by death.","The way you treat kittens in the early stages of it's life will render it's personality traits later in life."]

相关问题