getCurrentLocation method

Future<void> getCurrentLocation(
  1. TextEditingController controller
)

Implementation

Future<void> getCurrentLocation(TextEditingController controller) async {
  LocationPermission permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    /// Request location permission
    permission = await Geolocator.requestPermission();
  }

  if (permission == LocationPermission.whileInUse ||
      permission == LocationPermission.always) {
    /// Get the current position
    Position position = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.high,
    );

    /// Update the text controller with the current location
    controller.text = "${position.latitude}, ${position.longitude}";
  }
}