0

I'm new to flutter and even coding itself. I have a bit experience in Java and C# so I know datatypes and functions, etc.

My problem: I'm not realy firm in lists and maps right now but I have to send a https request and I will receive a list that contains other lists:

example:

[
 [
    "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"
  ],
]

I created a class to store these data:

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);
 }

I don´t need all data of the incoming lists. Only index 0,1,2,4,5,6,7,8,10,11,13 are needed

When I look at the cookbook (https://flutter.dev/docs/cookbook/networking/fetch-data) it tells me to create the album class (in my case the store class) but it works with a json and I don´t get a json. Maybe I missunderstand the cookbook but in general lists and maps in flutter is not my passion. Hopefully I provided all infomrations you need in a clear way. If not please ask me. My main issue is how to get the data I receive into the store class?? I appriciate any help from you.

sefre
  • 63
  • 5

2 Answers2

1

It's better to use json format for your network api. If you could not use json, you can use below code. Please make sure your api result format is exactly like above, i mean your requested index should be ready.

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});
}
Reza M
  • 544
  • 3
  • 10
  • Thanks a lot for your answer. Indeed I was able to parse the url response to json. thanks. – sefre May 18 '21 at 16:44
0

The response from your API can still be parsed as JSON with jsonDecode from the package dart:convert. But the example response you provided contains a trailing comma after the last array, which isn't valid JSON. Therefore, a regular expression can be used to replace the ,] with only ]. This effectively removes the leading commas everywhere, making it valid JSON.

jsonDecode will return your data in a list with lists inside, which can be used to create your objects.

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]);
}
hfxbse
  • 36
  • 1
  • 5
  • the trailing comma was a copy and paste issue because the list is a bit longer than the example shows. I managed to parse the url response to json so everything is fine. Thanks a lot. – sefre May 18 '21 at 16:45
  • Ah, I wasn't sure so I just wanted to make sure it will work haha. You may mark the accepted answer to make it easier for others to find the solution :) – hfxbse May 19 '21 at 12:03