pre final ad services and correct call the services async

This commit is contained in:
2026-02-20 09:51:03 +01:00
parent 8349e2b496
commit 46f7416781
7 changed files with 479 additions and 207 deletions

View File

@@ -0,0 +1,54 @@
import 'package:geolocator/geolocator.dart';
class GeolocationService {
late double latitude;
late double longitude;
late bool hasLocation;
Future<void> getCurrentLocation() async {
bool serviceEnabled = false;
LocationPermission permission;
try {
// 1. Prüfen, ob Standortdienste aktiviert sind
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Standortdienste sind deaktiviert.');
}
hasLocation = serviceEnabled;
// 2. Berechtigungen prüfen
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
hasLocation = true;
if (permission == LocationPermission.denied) {
hasLocation = false;
return Future.error('Berechtigung verweigert.');
}
}
// 3. Position abrufen
Position position = await Geolocator.getCurrentPosition(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
),
);
// 4. Standort über Backend-Proxy abrufen
latitude = position.latitude;
longitude = position.longitude;
if(latitude > 0 && longitude > 0) {
hasLocation = true;
}
} catch (e) {
print("Fehler beim Abrufen des Standorts: $e");
hasLocation = false;
}
}
}