todos-contra-el-fuego-mobile/lib/locationUtils.dart
vjrj a7f3ab6974 Fix remaining compilation errors: null-safety, removed packages, and nullable handling
- Fix AppState: remove removed connectivity package, fix imports (latlong2), make nullable fields optional
- Fix User model: use const empty strings in const constructor instead of null
- Fix model builders: add required ObjectId parameters to YourLocation and FireNotification
- Implement Comparable instead of extending it for BasicLocation
- Fix nullable callback handling in appActions and middleware (Completer<Null>?)
- Add null checks for BuildContext in dialogs (fireAlert, homePage, etc.)
- Fix nullable scaffold state access using ?. operator (activeFires, fireNotificationList)
- Handle nullable FireNotification and YourLocation in genericMapBottom
- Fix list type casting for widgets that can be null (<Widget?> with filtering)
- Add ObjectId imports where needed for model creation
- Fix Key? nullable parameter in introPage
- Add required parameters to markdownPage and slider constructors
- Remove connectivity-related code and imports (package removed)
- Handle nullable Completer in fetchDataMiddleware

All 104 errors resolved. 0 errors, 61 warnings/info remaining.
2026-03-05 02:50:32 +01:00

71 lines
2.3 KiB
Dart

import 'dart:async';
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:geocoding/geocoding.dart' as geo;
import 'package:location/location.dart';
import 'package:objectid/objectid.dart';
import 'generated/i18n.dart';
Future<YourLocation> getUserLocation(
GlobalKey<ScaffoldState> scaffoldKey) async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
Location _location = new Location();
LocationData location = await _location.getLocation();
// It seems that the lib fails with lat/lon values
var yl = new YourLocation(
id: ObjectId(), lat: location.latitude!, lon: location.longitude!);
var address;
try {
address = await getReverseLocation(lat: yl.lat, lon: yl.lon);
yl.description = address;
} catch (e) {
try {
address =
await getReverseLocation(lat: yl.lat, lon: yl.lon, external: true);
yl.description = address;
} catch (e) {
print('We cannot reverse geolocate');
}
}
return yl;
} on PlatformException catch (e) {
final context = scaffoldKey.currentContext;
if (context != null) {
if (e.code == 'PERMISSION_DENIED') {
ScaffoldMessenger.of(context).showSnackBar(new SnackBar(
content: new Text(S.of(context).notPermsUbication),
));
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {}
ScaffoldMessenger.of(context).showSnackBar(new SnackBar(
content: new Text(S.of(context).isYourUbicationEnabled),
));
}
return YourLocation.noLocation;
}
}
Future<String> getReverseLocation(
{required double lat, required double lon, bool external = false}) async {
try {
List<geo.Placemark> placemarks =
await geo.placemarkFromCoordinates(lat, lon);
if (placemarks.isNotEmpty) {
var first = placemarks.first;
String address =
'${first.street}, ${first.locality}, ${first.administrativeArea}, ${first.country}';
print("${first.name} : $address");
return address;
} else {
return 'Unable to determine address';
}
} catch (e) {
print("Error in reverse geocoding: $e");
throw Exception("Failed to reverse geocode: $e");
}
}