How to get current location on flutter sdk 1.2.1

Mateen Kiani picture Mateen Kiani · Feb 28, 2019 · Viewed 25k times · Source

How can i get current location on android device using flutter. I have tried both Location and GeoLocator plugins the GeoLocator 3.0.0 shows this error on debuging:

Launching lib\main.dart on Android SDK built for x86 in debug mode...

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:preDebugBuild'.

    Android dependency 'android.arch.lifecycle:runtime' has different version for the compile (1.0.0) and runtime (1.1.0) classpath. You should manually set the same version via DependencyResolution

Location 2.0.0 also throws an error on debugging. There is another plugin available named geoLocation but that is not compatible with dart 2.0. In such a situation how can i get location (longitude and latitude)[once]? Any help would be highly appreciated.

Answer

Abdul Rehman picture Abdul Rehman · May 21, 2019

Follow these steps to get location in both IOS and Android.

  1. Add geolocator: ^4.0.3 in your pubspec.yaml file below dependencies: tag
  2. Add import 'package:geolocator/geolocator.dart'; in your dart file where you need to get location
  3. In order to get location for android add following permissions in android project manifest file

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  1. For IOS you'll need to add following lines in your info.plist file under <dict> in IOS project

    <key>NSLocationWhenInUseUsageDescription</key>

    <string>This app needs access to location when open.</string>

  2. Function to get location

    void getLocation() async { Position position = await Geolocator .getCurrentPosition(desiredAccuracy: LocationAccuracy.high); print(position); }

Cheers!