Hi All,
I have a pro-account and In my project using the custom function generated by Code CoPilot..There is issue where the JSON output of google maps API always gives null output. The code is not working correctly as it always gives output as Null.
Can any provide some help or input?
The custom function code is:
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/auth/firebase_auth/auth_util.dart';
String? getCountryFromGeoLoc(dynamic sample1) {
/// MODIFY CODE ONLY BELOW THIS LINE
try {
// Parse the JSON response
Map<String, dynamic> data = json.decode(sample1);
// Extract the address components from the response
List<dynamic> results = data['results'];
if (results.isNotEmpty) {
List<dynamic> addressComponents = results[0]['address_components'];
// Find the country component
Map<String, dynamic>? countryComponent =
addressComponents.firstWhere((component) {
List<dynamic> types = component['types'];
return types.contains('country');
}, orElse: () => null);
// Extract the country name
if (countryComponent != null) {
return countryComponent['long_name'];
}
}
} catch (e) {
print('Error parsing JSON: $e');
}
return null;
/// MODIFY CODE ONLY ABOVE THIS LINE
}
JSON is below:
JSON being given is:
{
"plus_code": {
"compound_code": "P27Q+MCM New York, NY, USA",
"global_code": "87G8P27Q+MCM"
},
"results": [
{
"address_components": [
{
"long_name": "277",
"short_name": "277",
"types": [
"street_number"
]
},
{
"long_name": "Bedford Avenue",
"short_name": "Bedford Ave",
"types": [
"route"
]
},
{
"long_name": "Williamsburg",
"short_name": "Williamsburg",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "11211",
"short_name": "11211",
"types": [
"postal_code"
]
}
],
"formatted_address": "277 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry": {
"location": {
"lat": 40.7142205,
"lng": -73.9612903
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 40.71556948029149,
"lng": -73.95994131970849
},
"southwest": {
"lat": 40.7128715197085,
"lng": -73.9626392802915
}
}
},
"place_id": "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
"plus_code": {
"compound_code": "P27Q+MF Brooklyn, NY, USA",
"global_code": "87G8P27Q+MF"
},
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "279",
"short_name": "279",
"types": [
"street_number"
]
},
{
"long_name": "Bedford Avenue",
"short_name": "Bedford Ave",
"types": [
"route"
]
},
{
"long_name": "Williamsburg",
"short_name": "Williamsburg",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "11211",
"short_name": "11211",
"types": [
"postal_code"
]
},
{
"long_name": "4203",
"short_name": "4203",
"types": [
"postal_code_suffix"
]
}
],
"formatted_address": "279 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.7142628,
"lng": -73.9612131
},
"southwest": {
"lat": 40.7141534,
"lng": -73.9613792
}
},
"location": {
"lat": 40.7142015,
"lng": -73.96130769999999
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 40.7155570802915,
"lng": -73.95994716970849
},
"southwest": {
"lat": 40.7128591197085,
"lng": -73.96264513029149
}
}
},
"place_id": "ChIJRYYERGBZwokRAM4n1GlcYX4",
"types": [
"premise"
]
},
{
"address_components": [
{
"long_name": "277",
"short_name": "277",
"types": [
"street_number"
]
},
{
"long_name": "Bedford Avenue",
"short_name": "Bedford Ave",
"types": [
"route"
]
},
{
"long_name": "Williamsburg",
"short_name": "Williamsburg",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "11211",
"short_name": "11211",
"types": [
"postal_code"
]
}
],
"formatted_address": "277 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry": {
"location": {
"lat": 40.7142205,
"lng": -73.9612903
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 40.71556948029149,
"lng": -73.95994131970849
},
"southwest": {
"lat": 40.7128715197085,
"lng": -73.9626392802915
}
}
},
"place_id": "ChIJIdIK_DJZwokRImC6xolwWOM",
"plus_code": {
"compound_code": "P27Q+MF Brooklyn, NY, USA",
"global_code": "87G8P27Q+MF"
},
"types": [
"establishment",
"food",
"point_of_interest",
"restaurant"
]
},
{
"address_components": [
{
"long_name": "298",
"short_name": "298",
"types": [
"street_number"
]
},
{
"long_name": "Bedford Avenue",
"short_name": "Bedford Ave",
"types": [
"route"
]
},
{
"long_name": "Williamsburg",
"short_name": "Williamsburg",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "11249",
"short_name": "11249",
"types": [
"postal_code"
]
}
],
"formatted_address": "298 Bedford Ave, Brooklyn, NY 11249, USA",
"geometry": {
"location": {
"lat": 40.7142307,
"lng": -73.961477
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"northeast": {
"lat": 40.7155796802915,
"lng": -73.9601280197085
},
"southwest": {
"lat": 40.7128817197085,
"lng": -73.96282598029151
}
}
},
"place_id": "EigyOTggQmVkZm9yZCBBdmUsIEJyb29rbHluLCBOWSAxMTI0OSwgVVNBIhsSGQoUChIJ8ThWRGBZwokR3U1zUisk3LUQqgI",
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "291-275",
"short_name": "291-275",
"types": [
"street_number"
]
},
{
"long_name": "Bedford Avenue",
"short_name": "Bedford Ave",
"types": [
"route"
]
},
{
"long_name": "Williamsburg",
"short_name": "Williamsburg",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "11211",
"short_name": "11211",
"types": [
"postal_code"
]
}
],
"formatted_address": "291-275 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.7145061,
"lng": -73.96129719999999
},
"southwest": {
"lat": 40.713908,
"lng": -73.96168779999999
}
},
"location": {
"lat": 40.71420699999999,
"lng": -73.96149249999999
},
"location_type": "GEOMETRIC_CENTER",
"viewport": {
"northeast": {
"lat": 40.71555603029149,
"lng": -73.96014351970848
},
"southwest": {
"lat": 40.7128580697085,
"lng": -73.9628414802915
}
}
},
"place_id": "ChIJ8ThWRGBZwokR3E1zUisk3LU",
"types": [
"route"
]
},
{
"address_components": [
{
"long_name": "P27Q+MC",
"short_name": "P27Q+MC",
"types": [
"plus_code"
]
},
{
"long_name": "New York",
"short_name": "New York",
"types": [
"locality",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "P27Q+MC New York, NY, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.71425,
"lng": -73.96137499999999
},
"southwest": {
"lat": 40.714125,
"lng": -73.9615
}
},
"location": {
"lat": 40.714224,
"lng": -73.961452
},
"location_type": "GEOMETRIC_CENTER",
"viewport": {
"northeast": {
"lat": 40.71553648029149,
"lng": -73.96008851970849
},
"southwest": {
"lat": 40.71283851970849,
"lng": -73.96278648029151
}
}
},
"place_id": "GhIJWAIpsWtbREARHyv4bYh9UsA",
"plus_code": {
"compound_code": "P27Q+MC New York, NY, USA",
"global_code": "87G8P27Q+MC"
},
"types": [
"plus_code"
]
},
{
"address_components": [
{
"long_name": "South Williamsburg",
"short_name": "South Williamsburg",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "South Williamsburg, Brooklyn, NY, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.7167119,
"lng": -73.9420904
},
"southwest": {
"lat": 40.6984866,
"lng": -73.9699432
}
},
"location": {
"lat": 40.7043921,
"lng": -73.9565551
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 40.7167119,
"lng": -73.9420904
},
"southwest": {
"lat": 40.6984866,
"lng": -73.9699432
}
}
},
"place_id": "ChIJR3_ODdlbwokRYtN19kNtcuk",
"types": [
"neighborhood",
"political"
]
},
{
"address_components": [
{
"long_name": "11211",
"short_name": "11211",
"types": [
"postal_code"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "New York",
"short_name": "New York",
"types": [
"locality",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Brooklyn, NY 11211, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.7280089,
"lng": -73.9207299
},
"southwest": {
"lat": 40.7008331,
"lng": -73.9644697
}
},
"location": {
"lat": 40.7093358,
"lng": -73.9565551
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 40.7280089,
"lng": -73.9207299
},
"southwest": {
"lat": 40.7008331,
"lng": -73.9644697
}
}
},
"place_id": "ChIJvbEjlVdZwokR4KapM3WCFRw",
"types": [
"postal_code"
]
},
{
"address_components": [
{
"long_name": "Williamsburg",
"short_name": "Williamsburg",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Williamsburg, Brooklyn, NY, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.7251773,
"lng": -73.9369427
},
"southwest": {
"lat": 40.6979329,
"lng": -73.96984499999999
}
},
"location": {
"lat": 40.7081156,
"lng": -73.9570696
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 40.7251773,
"lng": -73.9369427
},
"southwest": {
"lat": 40.6979329,
"lng": -73.96984499999999
}
}
},
"place_id": "ChIJQSrBBv1bwokRbNfFHCnyeYI",
"types": [
"neighborhood",
"political"
]
},
{
"address_components": [
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Brooklyn, NY, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.7394209,
"lng": -73.8330411
},
"southwest": {
"lat": 40.551042,
"lng": -74.05663
}
},
"location": {
"lat": 40.6781784,
"lng": -73.9441579
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 40.7394209,
"lng": -73.8330411
},
"southwest": {
"lat": 40.551042,
"lng": -74.05663
}
}
},
"place_id": "ChIJCSF8lBZEwokRhngABHRcdoI",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"address_components": [
{
"long_name": "Kings County",
"short_name": "Kings County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Brooklyn",
"short_name": "Brooklyn",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Kings County, Brooklyn, NY, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.7394209,
"lng": -73.8330411
},
"southwest": {
"lat": 40.551042,
"lng": -74.05663
}
},
"location": {
"lat": 40.6528762,
"lng": -73.95949399999999
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 40.7394209,
"lng": -73.8330411
},
"southwest": {
"lat": 40.551042,
"lng": -74.05663
}
}
},
"place_id": "ChIJOwE7_GTtwokRs75rhW4_I6M",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"address_components": [
{
"long_name": "New York",
"short_name": "New York",
"types": [
"locality",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "New York, NY, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.9175771,
"lng": -73.70027209999999
},
"southwest": {
"lat": 40.4773991,
"lng": -74.25908989999999
}
},
"location": {
"lat": 40.7127753,
"lng": -74.0059728
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 40.9175771,
"lng": -73.70027209999999
},
"southwest": {
"lat": 40.4773991,
"lng": -74.25908989999999
}
}
},
"place_id": "ChIJOwg_06VPwokRYv534QaPC8g",
"types": [
"locality",
"political"
]
},
{
"address_components": [
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "New York, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 45.015861,
"lng": -71.777491
},
"southwest": {
"lat": 40.476578,
"lng": -79.7625901
}
},
"location": {
"lat": 43.2994285,
"lng": -74.21793260000001
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 45.015861,
"lng": -71.777491
},
"southwest": {
"lat": 40.476578,
"lng": -79.7625901
}
}
},
"place_id": "ChIJqaUj8fBLzEwRZ5UY3sHGz90",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"address_components": [
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "United States",
"geometry": {
"bounds": {
"northeast": {
"lat": 74.071038,
"lng": -66.885417
},
"southwest": {
"lat": 18.7763,
"lng": 166.9999999
}
},
"location": {
"lat": 38.7945952,
"lng": -106.5348379
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 74.071038,
"lng": -66.885417
},
"southwest": {
"lat": 18.7763,
"lng": 166.9999999
}
}
},
"place_id": "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",
"types": [
"country",
"political"
]
}
],
"status": "OK"
}