I have a form for users to submit new points on the map, which are populated on the map based on the latitude and longitude coordinates. I have a custom action that makes the API call, parses out the appropriate fields, and populates them in the action output variable. Then, in the action flow, I check for an existing document and, assuming there is no existing document, I create a new document and set the fields accordingly. Everything works right, except for the latitude and longitude coordinates. I can't get them to set, no matter what I do.
The custom action code is as follows:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:convert'; // For JSON decoding
import 'package:http/http.dart' as http; // For HTTP requests
Future<dynamic> reverseGeocodeToLatLng(
String street,
String city,
String state,
String zip,
String apiKey,
) async {
// Format the address
final address = [street, city, state, zip]
.where((part) => part.isNotEmpty) // Remove empty or null parts
.join(', ');
print('Formatted address: $address');
// Construct the API URL
final url = Uri.parse(
'https://maps.googleapis.com/maps/api/geocode/json?address=${Uri.encodeComponent(address)}&key=$apiKey');
print('API URL: $url');
try {
// Make the HTTP request
final response = await http.get(url);
if (response.statusCode == 200) {
// Parse the JSON response
final Map<String, dynamic> jsonResponse = jsonDecode(response.body);
if (jsonResponse['results'] != null &&
jsonResponse['results'].isNotEmpty) {
final location = jsonResponse['results'][0]['geometry']['location'];
// Extract latitude and longitude
final double lat = location['lat'] as double;
final double lng = location['lng'] as double;
print('Extracted Latitude: $lat');
print('Extracted Longitude: $lng');
// Create the LatLng object
final latLng = LatLng(lat, lng);
// Build the JSON result
final Map<String, dynamic> result = {
'latitude': lat,
'longitude': lng,
'latLng': {
'latitude': lat,
'longitude': lng,
},
};
// Print the result before returning
print('Final Result: $result');
// Return the JSON string
return jsonEncode(result);
} else {
// No results found
print('No results found for the address: $address');
return jsonEncode({'error': 'No results found'});
}
} else {
// API request failed
print('API request failed with status: ${response.statusCode}');
return jsonEncode({'error': 'API request failed'});
}
} catch (e) {
// Handle any other errors
print('Error occurred: $e');
return jsonEncode({'error': e.toString()});
}
}I can see in the console logs that the API call is successful and the variables are parsed appropriately. What I can't see though, is whether the action output is actually trying to populate the document or not.
Screenshots of the action flow to show what happens and how the variables are set:
If all of the text fields required for the firestore lookup AND the API call are set and not empty, then it queries firestore to check for an existing document.
If there is no existing document with the data from the fields, it will then call the reverseGeocodeToLatLng custom action to pull the latitude and longitude based on the address information the user entered in the form.
There are also Choice Chips for whether the user is affiliated with the address or not, and that determines which fields are set in the firestore document. Either way, the fields are essentially identical with the exception of a name and email field, which are irrelevant to this issue (could be wrong, I guess).
The document is created and the fields are set. latitude is set from the action output from the reverseGeocodeToLatLng action that happened earlier, and it's set from the $.latitude JSON path (which I confirmed is correct from the custom code as well as the debug log to make sure the call. is successfull and the appropriate data is returned.
The firestore document is created, the address information is populated, picture is uploaded (URL), etc. it's only the latitude and longitude that aren't populated. Am I missing something?