- Add @immutable to classes with ==/hashCode (homePage, basicLocation, fireNotification, yourLocation, monitoredAreas) - Fix camel_case_types (genericMap → GenericMap) - Fix avoid_dynamic_calls (firesApi - typed responses) - Fix use_build_context_synchronously (locationUtils) - Fix always_put_control_body_on_new_line (4 reducers) - Fix always_specify_types (placesAutocompleteUtils, reducers) - Fix eol_at_end_of_file (4 files) - Fix prefer_function_declarations_over_variables (introPage) - Fix flutter_style_todos (fireMapReducer) Build: APK generated, 0 errors, 0 warnings
75 lines
2.4 KiB
Dart
75 lines
2.4 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 (_) {
|
|
// 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');
|
|
}
|
|
}
|