todos-contra-el-fuego-mobile/lib/location_utils.dart
vjrj 30a89fc5c0 Fix 13 lint issues: immutability, imports, async/await, and code style
High-priority fixes (10 issues):
- assignment_to_final errors (7): Use copyWith() to respect immutability in YourLocation, active_fires.dart, generic_map.dart, location_utils.dart, and fetch_data_middleware.dart instead of direct assignment
- Fixed import paths (3): Updated falsePositiveTypes.dart → false_positive_types.dart and firesApi.dart → fires_api.dart in reducers.dart, fire_notification_actions.dart, fires_api.dart

Medium/Low priority fixes (3 issues):
- unnecessary_non_null_assertion: Removed redundant '!' in app_intro_page.dart line 54
- use_super_parameters: Updated app_intro_page.dart to use modern super(key: key) syntax
- no_logic_in_create_state: Fixed material_app_with_intro.dart by moving parameters to State instead of createState
- noop_primitive_operations: Removed redundant .toString() in string interpolation
- avoid_void_async: Changed void _selectLocation to Future<void> in places_autocomplete_utils.dart
- avoid_dynamic_calls: Fixed dynamic map access in fires_api.dart by properly casting intermediate values

Result: Reduced lint issues from 56 to 20 (all remaining are by-design: library_private_types_in_public_api and implementation_imports from external packages)
APK builds successfully with no errors.
2026-03-12 09:16:20 +01:00

75 lines
2.5 KiB
Dart

import 'dart:async';
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';
import 'models/your_location.dart';
Future<YourLocation> getUserLocation(
GlobalKey<ScaffoldState> scaffoldKey) async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
final Location location0 = Location();
final LocationData location = await location0.getLocation();
// It seems that the lib fails with lat/lon values
YourLocation yl = YourLocation(
id: ObjectId(), lat: location.latitude!, lon: location.longitude!);
String address;
try {
address = await getReverseLocation(lat: yl.lat, lon: yl.lon);
yl = yl.copyWith(description: address);
} catch (e) {
try {
address =
await getReverseLocation(lat: yl.lat, lon: yl.lon, external: true);
yl = yl.copyWith(description: address);
} catch (_) {
// Ignore - fallback already attempted
}
}
return yl;
} on PlatformException catch (e) {
final BuildContext? context = scaffoldKey.currentContext;
if (context != null) {
// ignore: use_build_context_synchronously
final S strings = S.of(context);
// ignore: use_build_context_synchronously
final ScaffoldMessengerState messenger = ScaffoldMessenger.of(context);
if (e.code == 'PERMISSION_DENIED') {
messenger.showSnackBar(SnackBar(
content: Text(strings.notPermsUbication),
));
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
// User selected "Don't ask again" - show settings prompt
}
messenger.showSnackBar(SnackBar(
content: Text(strings.isYourUbicationEnabled),
));
}
return YourLocation.noLocation;
}
}
Future<String> getReverseLocation(
{required double lat, required double lon, bool external = false}) async {
try {
final List<geo.Placemark> placemarks =
await geo.placemarkFromCoordinates(lat, lon);
if (placemarks.isNotEmpty) {
final geo.Placemark first = placemarks.first;
final String address =
'${first.street}, ${first.locality}, ${first.administrativeArea}, ${first.country}';
return address;
} else {
return 'Unable to determine address';
}
} catch (e) {
throw Exception('Failed to reverse geocode: $e');
}
}