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

标签 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
                }
            ],

最佳答案

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

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

关于dart - 如何将photo_reference值存储为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50257590/

相关文章:

dart - Flutter:如何验证 get 请求返回 html 的 api

flutter - Flutter _如何解决此键盘的显示错误

dart - Flutter http 错误类型 '_InternalLinkedHashMap<dynamic, dynamic>' 不是类型 'Map<String, String>' 的子类型

dart - StreamBuilder 子部件交互

flutter - Dart:意外文本 'late'

dart - 使用 Navigator.popUntil 和没有固定名称的路由

canvas - 神秘 flutter 警告 : Element is implicitly hidden?

flutter - 在显示和隐藏窗口小部件中的动画时,我在哪里出错?

dart - 在 Flutter 中导航屏幕时丢失数据

flutter - RiverPod - 如何在不在小部件中的 AsyncValue 上等待使用 FutureProvider