Skip to content

Instantly share code, notes, and snippets.

@Imperial-lord
Last active February 11, 2022 23:13
Show Gist options
  • Save Imperial-lord/1a80c086182d5d0c77218bf4548f58bb to your computer and use it in GitHub Desktop.
Save Imperial-lord/1a80c086182d5d0c77218bf4548f58bb to your computer and use it in GitHub Desktop.
We will capture the user location after ensuring required permissions are granted and store this location info in Shared Preferences
// Ensure all permissions are collected for Locations
Location _location = Location();
bool? _serviceEnabled;
PermissionStatus? _permissionGranted;
_serviceEnabled = await _location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await _location.requestService();
}
_permissionGranted = await _location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await _location.requestPermission();
}
// Get capture the current user location
LocationData _locationData = await _location.getLocation();
LatLng currentLatLng =
LatLng(_locationData.latitude!, _locationData.longitude!);
// Store the user location in sharedPreferences
sharedPreferences.setDouble('latitude', _locationData.latitude!);
sharedPreferences.setDouble('longitude', _locationData.longitude!);
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => const HomeManagement()),
(route) => false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment