Hi Team Greetings from my side 🔥🥰
Please I'm building a food delivery app and I have made a code to check if the user grant the app location so if the location permission is granted and location service is enable and this code we proceed to get the device current location and also the address but if location permission is denied this code need to get the last known location and and retrieve the address as well.. So basically retrieve the current location is working perfectly but the last known location isn't working. I mean isn't retrieving the last known located so please here is my code please help me debug or correct the code to get the last known location. Thanks in advance
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/actions/actions.dart' as action_blocks;
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'; // For converting coordinates to address
import 'package:geolocator/geolocator.dart'; // For accessing device location
Future<dynamic> getUserAddress() async {
try {
Position? position;
// Check if location services are enabled
bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
// Request location permission if not already granted
LocationPermission permission = await Geolocator.checkPermission();
// If location services are disabled or permission is denied, get the last known position
if (!isLocationServiceEnabled ||
permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
// Try to get the last known position
position = await Geolocator.getLastKnownPosition();
} else {
// If location services are enabled and permission is granted, get the current position
position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
);
}
// Handle the case where no position is available
if (position == null) {
return {
'status': 'error',
'message':
'Unable to retrieve location. No current or last known location available.'
};
}
// Retrieve the address from the obtained position
List<Placemark> placemarks =
await placemarkFromCoordinates(position.latitude, position.longitude);
if (placemarks.isNotEmpty) {
// Construct the address string
String address = (placemarks[0].street ?? '') +
", " +
(placemarks[0].subLocality ?? '') +
", " +
(placemarks[0].locality ?? '') +
", " +
(placemarks[0].administrativeArea ?? '') +
", " +
(placemarks[0].country ?? '');
// Return the result in a JSON-like format
return {
'status': 'success',
'latitude': position.latitude,
'longitude': position.longitude,
'address': address,
};
} else {
return {
'status': 'error',
'message': 'No address found for the given location.'
};
}
} catch (e) {
// Return an error message if an exception occurs
return {'status': 'error', 'message': 'Failed to retrieve address: $e'};
}
}