Hey,
Alan Lucena had a question on the discord server and I thought this might be useful for someone else so I'm posting it here ๐
The need was to get the user's address from the devices current locations Latitude and Longitude.
This action uses the geocoding package.
The code for the custom action:
// Automatic FlutterFlow imports
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 'package:geocoding/geocoding.dart';
Future<String> getUserAddress(LatLng userCurrentLocation) async {
try {
List<Placemark> placemarks = await placemarkFromCoordinates(
userCurrentLocation.latitude, userCurrentLocation.longitude);
// Check if placemarks list is not empty and the first placemark is not null
if (placemarks.isNotEmpty && placemarks[0] != null) {
// Access placemark properties safely
String address = (placemarks[0].street ?? '') +
", " +
(placemarks[0].subLocality ?? '') +
", " +
(placemarks[0].locality ?? '') +
", " +
(placemarks[0].administrativeArea ?? '') +
", " +
(placemarks[0].country ?? '');
return address;
} else {
return 'No address found';
}
} catch (e) {
return 'Error: $e';
}
}
The parameters:
Hope this helps, have an amazing day everyone!