将 https 请求带入对象

bring an https request into an object

我刚接触 flutter 甚至自己编码。我在 Java 和 C# 方面有一点经验,所以我知道数据类型和函数等。

我的问题:我现在对列表和地图不是很确定,但我必须发送一个 https 请求,我将收到一个包含其他列表的列表:

示例:

[
 [
    "60",
    "49.142000",
    "9.362000",
    8,
    "Obersulmer Kachelofenbau",
    "Am Seebach 6",
    "74182",
    "Obersulm-Willsbach",
    "www.obersulmer-kachelofenbau.de",
    "",
    "07134 - 5 10 36 ",
    "info@obersulmer-kachelofenbau.de",
    "1557919527",
    "DE"
  ],
[
    "48",
    "48.917000",
    "9.425000",
    26,
    "K\u00f6gel Schornsteine GmbH",
    "Donaustra\u00dfe 17 - 19",
    "71522",
    "Backnang",
    "www.koegel-schornsteine.de",
    "",
    "07191 95255-40",
    "info@koegel-schornsteine.de",
    "1557989245",
    "DE"
  ],
]

我创建了一个 class 来存储这些数据:

class Store {
  final String id;
  final double lat;
  final double long;
  final String name;
  final String street;
  final String zip;
  final String city;
  final String web;
  final String phone;
  final String mail;
  final String countryCode;

  Store(this.id, this.lat, this.long, this.name, this.street, this.zip,
      this.city, this.web, this.phone, this.mail, this.countryCode);
 }

我不需要传入列表的所有数据。只需要索引 0,1,2,4,5,6,7,8,10,11,13

当我看食谱时(https://flutter.dev/docs/cookbook/networking/fetch-data) 它告诉我创建专辑 class(在我的例子中是商店 class),但它与 json 一起使用,但我没有得到 json。也许我误解了这本食谱,但总的来说,颤振中的列表和地图并不是我的热情。 希望我清楚地提供了您需要的所有信息。如果没有请问我。 我的主要问题是如何将收到的数据放入商店 class??我感谢你的帮助。

您的网络 api 最好使用 json 格式。 如果你不能使用 json,你可以使用下面的代码。请确保您的 api 结果格式与上面完全相同,我的意思是您请求的索引应该准备就绪。

void main() async {
  List<List> apiResults = [
    [
      "60",
      "49.142000",
      "9.362000",
      8,
      "Obersulmer Kachelofenbau",
      "Am Seebach 6",
      "74182",
      "Obersulm-Willsbach",
      "www.obersulmer-kachelofenbau.de",
      "",
      "07134 - 5 10 36 ",
      "info@obersulmer-kachelofenbau.de",
      "1557919527",
      "DE"
    ],
    [
      "48",
      "48.917000",
      "9.425000",
      26,
      "K\u00f6gel Schornsteine GmbH",
      "Donaustra\u00dfe 17 - 19",
      "71522",
      "Backnang",
      "www.koegel-schornsteine.de",
      "",
      "07191 95255-40",
      "info@koegel-schornsteine.de",
      "1557989245",
      "DE"
    ],
  ];

  List<Store> stores = [];

 
  for (var item in apiResults) {
    stores.add(Store(
      id: item[0],
      lat: double.parse(item[1]),
      long: double.parse(item[2]),
      name: item[4],
      street: item[5],
      zip: item[6],
      city: item[7],
      web: item[8],
      phone: item[10],
      mail: item[11],
      countryCode: item[13],
    ));
  }
  
  print(stores);
}

class Store {
  final String? id;
  final double? lat;
  final double? long;
  final String? name;
  final String? street;
  final String? zip;
  final String? city;
  final String? web;
  final String? phone;
  final String? mail;
  final String? countryCode;

  Store(
      {this.id,
      this.lat,
      this.long,
      this.name,
      this.street,
      this.zip,
      this.city,
      this.web,
      this.phone,
      this.mail,
      this.countryCode});
}

您的 API 的响应仍然可以被解析为 JSON 和包 dart:convert 中的 jsonDecode。但是您提供的示例响应在最后一个数组之后包含一个尾随逗号,这是无效的 JSON。因此,可以使用正则表达式将,]替换为仅]。这有效地删除了所有地方的前导逗号,使其有效 JSON.

jsonDecode 将 return 你的数据放在一个列表中,里面有列表,可以用来创建你的对象。

import 'dart:convert';

parse() {
  String response = '''[ 
    [
      "60",
      "49.142000",
      "9.362000",
      8,
      "Obersulmer Kachelofenbau",
      "Am Seebach 6",
      "74182",
      "Obersulm-Willsbach",
      "www.obersulmer-kachelofenbau.de",
      "",
      "07134 - 5 10 36 ",
      "info@obersulmer-kachelofenbau.de",
      "1557919527",
      "DE",
    ],
    [
      "48",
      "48.917000",
      "9.425000",
      26,
      "K\u00f6gel Schornsteine GmbH",
      "Donaustra\u00dfe 17 - 19",
      "71522",
      "Backnang",
      "www.koegel-schornsteine.de",
      "",
      "07191 95255-40",
      "info@koegel-schornsteine.de",
      "1557989245",
      "DE",
    ],
  ]''';
  
  response = response.replaceAll(new RegExp(r',( +|\n)( +)]'), ']');
  
  return jsonDecode(response);
}

void main(List<String> arguments) {
  var json = parse();
  print(json[0]);
  print(json[1]);
}