Asmoun
Asmoun

Reputation: 1747

Flutter location permission issues in android manifest file

my Flutter code :

 onPressed: () async {
                  Geolocator
                      .getCurrentPosition(desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
                      .then((Position position) {
                    setState(() {
                      _currentPosition = position;
                      print ( "LAT: ${_currentPosition.latitude}, LNG: ${_currentPosition.longitude}");
                    });
                  }).catchError((e) {
                    print(e);
                  });
                },

it show the following :

`No location permissions are defined in the manifest. Make sure at least CCESS_FINE_LOCATION` or ACCESS_COARSE_LOCATION are defined in the manifest.

even thought I have added the permission in the manifest

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.agent">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
   <application
        android:label="agent"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

and I have the following errors that I cannot fix : enter image description here

Attribute android:icon is not allowed here Unresolved class 'MainActivity'

Upvotes: 1

Views: 4698

Answers (4)

Mahmoud Salah Eldin
Mahmoud Salah Eldin

Reputation: 2098

Add this lines inside

android->app->src->main->AndroidManifest.xml

android->app->src->profile->AndroidManifest.xml

android->app->src->debug->AndroidManifest.xml

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

<!-- Recommended for Android 9 (API level 28) and lower. -->
<!-- Required for Android 10 (API level 29) and higher. -->
<service
   android:name="MyNavigationService"
   android:foregroundServiceType="location" ... >
<!-- Any inner elements would go here. -->
</service>

Read this article

Upvotes: 1

Guillem Puche
Guillem Puche

Reputation: 1404

I add some information below to the @OmarMohamoud's answer.

You can check the geolocator package docs. It talks about main 2 points

  • For AndroidX version of Android Support Libraries, add 2 properties and check the SDK version.
  • Add location permissions.

If you are in Android +10 and "if you want to continue receiving updates even when your App is running in the background" according to the docs, you have to add this permission:

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

Check carefully the docs.

Upvotes: 1

Omar Mahmoud
Omar Mahmoud

Reputation: 3067

desiredAccuracy: LocationAccuracy.best needs both

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

Upvotes: 5

ch271828n
ch271828n

Reputation: 17597

Firstly, try gradle sync. As is suggested here:

  1. Sync gradle from android studio (must required step)
  2. flutter clean
  3. flutter pub get
  4. flutter run

Secondly, if it still does not work, it may be caused by this. To solve this, please use permission_handler package to request the Permission.location or other similar permissions, before you actually use your geo plugin.

Upvotes: 1

Related Questions