todos-contra-el-fuego-mobile/lib/locationUtils.dart
vjrj ea588a9753 refactor: clean up 55 lint warnings and issues
- Remove all 31 avoid_print debug statements across codebase
- Fix 8 critical warnings (unused variables, unreachable defaults, etc)
- Update deprecated APIs: launch() → launchUrl(), textScaleFactor → textScaler
- Replace deprecated surfaceVariant → surfaceContainerHighest in Material 3 themes
- Refactor switch statements to use modern pattern matching
- Remove unused code: _showDialog, _initNoLocation, _getAnchorOffset
- Fix use_build_context_synchronously by adding mounted checks
- Add ignore_for_file annotations to generated JSON serialization files
- Fix type annotations in appState.dart and models

Warnings reduced from 301 to 246 issues (8 → 0 critical warnings).
Build verified: app-production-debug.apk (160MB) compiles successfully.
Zero type errors, Dart analysis clean for critical issues.
2026-03-06 22:47:27 +01:00

68 lines
2.2 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/yourLocation.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
final YourLocation yl = YourLocation(
id: ObjectId(), lat: location.latitude!, lon: location.longitude!);
String 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) {
}
}
return yl;
} on PlatformException catch (e) {
final BuildContext? context = scaffoldKey.currentContext;
if (context != null) {
if (e.code == 'PERMISSION_DENIED') {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(S.of(context).notPermsUbication),
));
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {}
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(S.of(context).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');
}
}