Skip to content

Instantly share code, notes, and snippets.

@tauheed0007
Last active April 29, 2022 19:45
Show Gist options
  • Save tauheed0007/990796af7ba921a9b914945e98341f38 to your computer and use it in GitHub Desktop.
Save tauheed0007/990796af7ba921a9b914945e98341f38 to your computer and use it in GitHub Desktop.
this is latest method to access location with geolocator dependency to find location and its the error free version of londonappbrewery Angela method
//import
import 'package:geolocator/geolocator.dart';
//dependency
geolocator: ^8.2.0
//manifest file under direct tag of applicatioon after android 10 it is necessary to use background location
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
class _LoadingScreenState extends State<LoadingScreen> {
Future<Position> determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled don't continue
// accessing the position and request users of the
// App to enable the location services.
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
// When we reach here, permissions are granted and we can
// continue accessing the position of the device.
return await Geolocator.getCurrentPosition();
}
void locatePosition() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
print(position);
// Ask permission from device
/* Future<void> requestPermission() async {
await Permission.location.request();*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment