Flutter - ‹асинхронная подвеска› с пакетом геокодера

Мне нужно получить название города по долготе и широте. Я не нашел ни одного пакета для этого, поэтому я использую пакет местоположения, чтобы получить долгую и широту, и использовал геокодер для получения названия города.

Но у меня сохраняется эта ошибка и нет результата:

[ОШИБКА: flutter / lib / ui / ui_dart_state.cc (177)] Необработанное исключение: PlatformException (failed, Failed, null, null)

и следующий за ним на консоли: ‹асинхронная приостановка›

Вот мой код:

class _ProfileScreenState extends State<ExploreScreen> {
  dynamic currentLocation;
  double userLongitude;
  double userLatitude;
  Coordinates coordinates;
  var addresses;
  var first;

  @override
  initState() {
    super.initState();
    _getCurrentLongAndLat();
    _getCurrentPosition(userLatitude, userLatitude);
  }

  Future _getCurrentLongAndLat() async {
    currentLocation = LocationData;
    var error;
    var location = new Location();

    try {
      currentLocation = await location.getLocation();
      userLatitude = currentLocation.latitude;
      userLongitude = currentLocation.longitude;
      print('$userLatitude $userLatitude');
    } on PlatformException catch (e) {
      if (e.code == 'PERMISSION_DENIED') {
        error = 'Permission denied';
      }
      currentLocation = null;
    }
  }

  Future _getCurrentPosition(double long, double lat) async {
    coordinates = new Coordinates(userLatitude, userLongitude);
    addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
    print(addresses.first);
  }
}

person KAMDeveloper    schedule 24.11.2020    source источник


Ответы (1)


Я понял, что иногда флаттер-пакеты геолокации и локации конфликтуют друг с другом.

Попробуйте использовать геолокатор, чтобы узнать ваше текущее местоположение:

Future _getCurrentPosition() async {
    Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high).then((Position position) {
      setState(() {
        _currentPosition = position;
        userLongitude = position.longitude;
        userLatitude = position.latitude;
        
      });
    });
  }

Это будущее, поэтому вы можете использовать await, чтобы действительно дождаться результатов, а после этого вызвать функцию getAddress с userLat и userLng.

person justin0060    schedule 24.11.2020