sefre
sefre

Reputation: 93

bring an https request into an object

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

Upvotes: 0

Views: 140

Answers (2)

hfxbse
hfxbse

Reputation: 36

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 ",
      "[email protected]",
      "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",
      "[email protected]",
      "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]);
}

Upvotes: 0

Reza M
Reza M

Reputation: 573

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 ",
      "[email protected]",
      "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",
      "[email protected]",
      "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});
}

Upvotes: 1

Related Questions