2

I want to locate the user and send his location coordinates via api to the server, Please answer with code, I've tried library geolocator: ^7.7.0

void getCurrentLocation() async {
     Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
     var lat = position.latitude;
     var long = position.longitude;
 print("Latitude: $lat and Longitude: $long");
   }

This code is not working , Error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found for method getCurrentPosition on channel flutter.baseflow.com/geolocator)
E/flutter (25476): #0      MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:156
E/flutter (25476): <asynchronous suspension>
E/flutter (25476): #1      MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:128:27)
E/flutter (25476): <asynchronous suspension>
E/flutter (25476): #2      getCurrentLocation
package:check_time/Screens/fingerprint_Screen.dart:84
E/flutter (25476): <asynchronous suspension>

5 Answers5

1

Build the app again. Try this code.

    Future<void> getCurrentLocation() async {
      Position position = await Geolocator.getCurrentPosition(
                           desiredAccuracy:LocationAccuracy.high);
      double lat = position.latitude;
      double long = position.longitude;
      print("Latitude: $lat and Longitude: $long");
  }
  • not worck * What went wrong: Execution failed for task ':geolocator_android:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 30s Exception: Gradle task assembleDebug failed with exit code 1 –  Oct 21 '21 at 10:10
  • change the geolocator version to 7.6.2 – Rudrransh Saxena Oct 21 '21 at 10:14
  • did not work * What went wrong: Execution failed for task ':geolocator_android:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 10s Exception: Gradle task assembleDebug failed with exit code 1 –  Oct 21 '21 at 10:18
0

Try restarting the application! should work from there onwards. Hot restart wont work when u add plugins.

Afzal
  • 291
  • 3
  • 5
  • no longer working * What went wrong: Execution failed for task ':geolocator_android:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 49s Exception: Gradle task assembleDebug failed with exit code 1 –  Oct 21 '21 at 10:07
  • did u follow the instructions for android? try flutter clean too! – Afzal Oct 21 '21 at 10:16
  • update compileSdkVersion 31 here android\app\build.gradle – Rutvik Moradiya Feb 28 '22 at 13:29
0

This error usually occurs if you haven't made changes to the app as per the required app platform.

i.e, android, iOS, Web

GOTO the package in package description under Usage

follow the implementation steps for the platform you are building

enter image description here

Vicky Salunkhe
  • 9,869
  • 6
  • 42
  • 59
0

Try below code hope its helpful to you, refer geolocator here and geocoding here for getting the latitude and longitude and your location name

Create Position and one variable for your current position and init() mathod

Position? _currentPosition;
  String? _currentAddress;
  void initState() {
    super.initState();
    getCurrentLocation();
  }

Function for get Latitude and Longitude

 getCurrentLocation() {
    Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.best,
            forceAndroidLocationManager: true)
        .then((Position position) {
      setState(() {
        _currentPosition = position;
         print(position.latitude);
         print(position.longitude);
        _getAddressFromLatLng();
      });
    }).catchError((e) {
      print(e);
    });
  }

Function for location from your latitude and longitude

_getAddressFromLatLng() async {
    try {
      List<Placemark> placemarks = await placemarkFromCoordinates(
          _currentPosition!.latitude, _currentPosition!.longitude);

      Placemark place = placemarks[0];

      setState(() {
        _currentAddress = "${place.subLocality}";//here you can used place.country and other things also
        print(_currentAddress);
      });
    } catch (e) {
      print(e);
    }
  }

Your Widget:

Column(
      children: [
        if (_currentPosition != null && _currentAddress != null)
          Text(
            _currentAddress!,
            style: TextStyle(
              fontWeight: FontWeight.bold,
              letterSpacing: .5,
              fontSize: 15,
              color: Colors.black,
            ),
          ),
      ],
    )

Add below lines inside android\app\src\main\AndroidManifest.xml file above of the application tag

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

Go to android\app\build.gradle file and change below changes

compileSdkVersion 31
minSdkVersion 21
targetSdkVersion 29
Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
  • * What went wrong: Execution failed for task ':geolocator_android:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 32s Exception: Gradle task assembleDebug failed with exit code 1 –  Oct 21 '21 at 11:17
  • @HamZa see my updated answer and change some code in `android\app\build.gradle` file and `re-run` your app and let me know – Ravindra S. Patil Oct 21 '21 at 11:58
0

There are some helpful steps to be followed by You which is mentioned on readme of package. Update

android\app\build.gradle

android {
  compileSdkVersion 31

  ...
}