Result.fromJson constructor

Result.fromJson(
  1. Map<String, dynamic> json
)

Creates a Result from Google Places API JSON response.

Provides sensible defaults for missing values (empty strings, false, empty lists, etc.)

Implementation

factory Result.fromJson(Map<String, dynamic> json) {
  return Result(
    addressComponents: json["address_components"] != null
        ? List<AddressComponent>.from(json["address_components"]
            .map((x) => AddressComponent.fromJson(x)))
        : [], // Default to an empty list if null
    adrAddress:
        json["adr_address"] ?? "", // Default to an empty string if null
    businessStatus:
        json["business_status"] ?? "", // Default to an empty string if null
    curbsidePickup:
        json["curbside_pickup"] ?? false, // Default to false if null
    currentOpeningHours: json["current_opening_hours"] != null
        ? CurrentOpeningHours.fromJson(json["current_opening_hours"])
        : null, // Null if not present
    delivery: json["delivery"] ?? false, // Default to false if null
    dineIn: json["dine_in"] ?? false, // Default to false if null
    formattedAddress:
        json["formatted_address"] ?? "", // Default to an empty string if null
    formattedPhoneNumber: json["formatted_phone_number"] ??
        "", // Default to an empty string if null
    geometry: json["geometry"] != null
        ? Geometry.fromJson(json["geometry"])
        : Geometry(
            location: Location(lat: 0, lng: 0),
            viewport: Viewport(
                northeast: Location(lat: 0, lng: 0),
                southwest: Location(
                    lat: 0, lng: 0))), // Default empty Geometry object
    icon: json["icon"] ?? "", // Default to an empty string if null
    iconBackgroundColor: json["icon_background_color"] ??
        "", // Default to an empty string if null
    iconMaskBaseUri: json["icon_mask_base_uri"] ??
        "", // Default to an empty string if null
    internationalPhoneNumber: json["international_phone_number"] ??
        "", // Default to an empty string if null
    name: json["name"] ?? "", // Default to an empty string if null
    openingHours: json["opening_hours"] != null
        ? OpeningHours.fromJson(json["opening_hours"])
        : null, // Null if not present
    photos: json["photos"] != null
        ? List<Photo>.from(json["photos"].map((x) => Photo.fromJson(x)))
        : [], // Default to an empty list if null
    placeId: json["place_id"] ?? "", // Default to an empty string if null
    plusCode: json["plus_code"] != null
        ? PlusCode.fromJson(json["plus_code"])
        : null, // Null if not present
    priceLevel: json["price_level"], // Can remain null
    rating: json["rating"]?.toDouble(), // Can remain null
    reference: json["reference"] ?? "", // Default to an empty string if null
    reservable: json["reservable"] ?? false, // Default to false if null
    reviews: json["reviews"] != null
        ? List<Review>.from(json["reviews"].map((x) => Review.fromJson(x)))
        : [], // Default to an empty list if null
  );
}