如何将photo_reference值存储为字符串?

时间:2018-05-09 15:59:46

标签: dart

我正在尝试使用dart构建我的第一个Android应用。该应用程序将理想地拉出附近的结果,但我无法与他们一起获取图片。如何在阵列“照片”中引用照片参考? Dart非常混乱。

import 'package:meta/meta.dart';

class Casino{
  Casino({
    @required this.name,
    @required this.id,
    @required this.photos,
    @required this.overview,
    this.favored,
  });

  String name,id,overview,photos;
  bool favored;

  Casino.fromJson(Map json)
      : name = json["name"],
        photos = json["photos"].getString("photo_reference"),
        id = json["place_id"].toString(),
        overview = json["overview"],
        favored = false;

}

引用的地方:

Widget build(BuildContext context){
    return Card(
      child: Container(
        height:200.0,
        padding: EdgeInsets.all(10.0),
        child: Row(
          children: <Widget>[
            casinoState.photos != null
            ? Hero(
              child: Image.network(
                "https://maps.googleapis.com/maps/api/place/photo?photoreference=${casinoState.photos}&sensor=false&maxheight=200&maxwidth=150&key=$key"),
              tag:casinoState.id,
              )
          :Container(),

JSON格式:

{
    "html_attributions": [],
    "results": [
        {
            "geometry": {
                "location": {
                    "lat": 40.1186169,
                    "lng": -74.9532456
                },
                "viewport": {
                    "northeast": {
                        "lat": 40.1199658802915,
                        "lng": -74.95189661970849
                    },
                    "southwest": {
                        "lat": 40.1172679197085,
                        "lng": -74.95459458029151
                    }
                }
            },
            "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
            "id": "080658566051e777f8c81b9a80db271629fd76a3",
            "name": "Parx Casino",
            "opening_hours": {
                "open_now": true,
                "weekday_text": []
            },
            "photos": [
                {
                    "height": 1152,
                    "html_attributions": [
                        "<a href=\"https://maps.google.com/maps/contrib/117948925865341354264/photos\">Parx Casino</a>"
                    ],
                    "photo_reference": "CmRaAAAABWy8pB7dKcz-M7iOpSdHMB1xmrN_EmFtxOh02aVemQsLdx4CqCsIOMIGKf6sHiEH2ukIJRNtF4b_NQ4SoXe-c65JxVnqo798RCyIjut9cq5pcquMx4-_D1TDPFjcLFEpEhDtNZHxFvLAeo6UESFM5vGMGhTuy6b3LoyD_KhCXudBmBHWaYlVmA",
                    "width": 2048
                }
            ],

1 个答案:

答案 0 :(得分:0)

photos包含一个数组。要获取第一个元素,请使用[0]

photos = json["photos"][0]["photo_reference"],