0

I m sorry for asking like those simple questions. because I m noob to json and flutter. I just need to understand this code segment. Please help.

var placeId = json['candidates'][0]['place_id'] as String;

I just found this code part when I need to get placeId for a place in google map using API_KEY in flutter project. This is the full code part of it.

  Future<String> getPlaceId(String input) async {
final String url =
    'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=$input&inputtype=textquery&key=$key';
var response = await http.get(Uri.parse(
    'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=$input&inputtype=textquery&key=$key'));
var json = convert.jsonDecode(response.body);

var placeId = json['candidates'][0]['place_id'] as String;
print(placeId);
return placeId;}

I found this code part from this video https://youtu.be/tfFByL7F-00

I need to know what is ['candidates'][0] and what is ['place_id'] how can I found those properties for place id. Are there more properties for placeId. where can I learn this type of code json['candidates'][0]['place_id'].

Thank you :)

Jack Y
  • 75
  • 7

1 Answers1

1

That code mean, it will access array candidates index number 0, and get place_id of candidates index number 0 {which is fisrt object in that array}, and then return the value as a String. So its something like this :

{
   "status": 200,
   "candidates": [
      {
         "place_id": 1
      },
      {
         "place_id": 2
      }
   ]
}

It will return 1 as its value

Ananda Pramono
  • 899
  • 1
  • 6
  • 18