import 'package:geolocator/geolocator.dart';
class Location {
double ? lat;
double ? lon;
void getLocation() async {
Position position = await _determinePosition();
lat = position.latitude;
lon = position.longitude;
}
Future < Position > _determinePosition() async {
LocationPermission permission;
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Permission Denied');
}
}
return await Geolocator.getCurrentPosition();
}
}
Asked
Active
Viewed 47 times
-1

Pragnesh Ghoda シ
- 8,318
- 3
- 25
- 40

Vipin
- 21
- 2
-
are you trying to get the location from map or from the background? Have you added required dependencies in `pubspec.yaml` – Pragnesh Ghoda シ Jun 08 '22 at 05:14
-
Try to follow this link and check if you have missed any steps https://mobikul.com/fetch-current-location-in-flutter/ – Pragnesh Ghoda シ Jun 08 '22 at 05:15
2 Answers
0
Important thing is : Add the permission in manifest . Use permission handler and take permission from user. After checking that we have permission, use the geolocator.

Behzad Shabanifard
- 68
- 6
0
You have to await
an async
method, otherwise you don't lknow whether it's done yet. Since you managed to not return a Future<>
, you cannot await
your method. Matter of fact you didn't even try, otherwise you would have gotten a compiler error at that point.
Future<void> getLocation() async {
Position position = await _determinePosition();
lat = position.latitude;
lon = position.longitude;
}
Now you need to find where you call this method and then await
it. Only then your values will be filled.

nvoigt
- 75,013
- 26
- 93
- 142
-
Thanks, It solves the problem, now i can fetch the value of lat and lon from getLocation, but there's one problem now, how can i call the method of getLocation, whenever i tries to print tha getLocation method, it will show Future instance of getLocation – Vipin Jun 08 '22 at 06:16
-
Indeed. You need to use `await` to get the actual value from a future. You already did that twice in your code. See: https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it – nvoigt Jun 08 '22 at 06:20