【问题标题】:what is the meaning of "var placeId = json['candidates'][0]['place_id'] as String;" in flutter google maps?“var placeId = json['candidates'][0]['place_id'] as String;”是什么意思在颤动的谷歌地图中?
【发布时间】:2022-01-20 03:35:41
【问题描述】:

我很抱歉问这些简单的问题。因为我对 json 很陌生,而且会颤抖。 我只需要了解这个代码段。请帮忙。

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

当我需要在 Flutter 项目中使用 API_KEY 获取谷歌地图中某个地点的 placeId 时,我刚刚找到了这个代码部分。这是它的完整代码部分。

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

我从视频https://youtu.be/tfFByL7F-00找到了这段代码

我需要知道什么是 ['candidates'][0] 以及什么是 ['place_id'] 我如何才能找到这些地点 ID 的属性。 placeId 是否有更多属性。 我在哪里可以学习这种类型的代码 json['candidates'][0]['place_id'].

谢谢你:)

【问题讨论】:

    标签: json flutter json-deserialization google-maps-flutter


    【解决方案1】:

    该代码的意思是,它将访问索引号 0 的数组 candidates,并获取索引号为 0 的候选对象的 place_id {这是该数组中的第一个对象},然后将值作为字符串返回。所以它是这样的:

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

    它会返回 1 作为它的值

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 2020-04-02
      • 2022-01-15
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 2011-06-20
      • 2014-01-06
      • 1970-01-01
      相关资源
      最近更新 更多