skip to Main Content

I have the following problem.
I’m building a flutter app which needs permission to get the location.
The app should request every permission that it needs when the app starts.
For this i use the package: permission_handler

This is my Code which i use to try getting my idea:

void main() {
  runApp(const MyApp());
  requestPermissions();
}
void requestPermissions() async {
  bool reqSuc = false;
  List<Permission> permissions = [
    Permission.location,
  ];


  for (Permission permission in permissions) {
    if (await permission.isGranted) {
      if (kDebugMode) {
        logger.i("Permission: $permission already granted");
      }
      reqSuc = true;
      continue;
    } else if (await permission.isDenied) {
      PermissionStatus permissionsStatus = await permission.request();
      if (permissionsStatus.isGranted) {
        if (kDebugMode) {
          logger.i("Permission: $permission already granted");
        }
        reqSuc = true;
      } else if (permissionsStatus.isPermanentlyDenied) {
        if (kDebugMode) {
          logger.i("Permission: $permission is permanently denied");
        }
        reqSuc = false;
      }
    }
  }
  if (reqSuc == false) {
    openAppSettings();
  }

Also I put following permissions in the AndroidManifest.xml:

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

But if I try to run this code im getting the Exception which i meantioned in the title:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(PermissionHandler.PermissionManager, Unable to detect current Android Activity., null, null)

Is there a way to fix this excpetion or another way to request the permissions at the start of the app?

Edit: Is there a way to ensure that the .MainActivity is loaded before requsting the permissions?

3

Answers


  1. add WidgetsFlutterBinding.ensureInitialized();

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      requestPermissions();
      runApp(const MyApp());
    }
    

    RUN:

    flutter clean
    flutter pub get
    
    Login or Signup to reply.
  2. add this line of code to your main.dart

    await PermissionHandler().requestPermissions([PermissionGroup.location]);

    Replace location with the permission you are requesting.

    Make sure that the Flutter project is properly linked to the Android project. You can do this by running the following command in your project directory:

    flutter clean && flutter pub get && flutter run

    Login or Signup to reply.
  3. Check this:

      Future<void> checkLocation() async {
        Location location = Location();
        bool? serviceEnabled;
        PermissionStatus? permissionGranted;
        serviceEnabled = await location.serviceEnabled();
        if (!serviceEnabled) {
          serviceEnabled = await location.requestService();
        }
    
        permissionGranted = await location.hasPermission();
        if (permissionGranted == PermissionStatus.denied) {
          permissionGranted = await location.requestPermission();
        }
      }
    
    

    Run thefunction in your main app

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      checkLocation();
      runApp(const MyApp());
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search