Granting the location permission using permission_handler

30 views Asked by At

I have create a demo app for the location permission but i found the issue on MissingPluginException when using permission_handler_11.3.0.

enter image description here

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

const platform = MethodChannel('your_channel_name');

Future<void> callNativeMethod() async {
  try {
     await platform.invokeMethod('nativeMethod');
  } catch (e) {
    print('Error calling native method: $e');
  }
}

class MyApp extends StatelessWidget {
 const MyApp({super.key});

 @override
 Widget build(BuildContext context) {
    return MaterialApp(
       title: 'Text and Button Example',
       theme: ThemeData(
              primarySwatch: Colors.blue,
       ),
       home: MyHomePage(),
   );
   }
  }

 class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
  final permissionLocation = Permission.location;

  return Scaffold(
      appBar: AppBar(
      title: Text('Demo App'),
      ),
      body: Center(
          child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
                  Text(
                      'Hello, Flutter!',
                 style: TextStyle(fontSize: 24),
            ),
         SizedBox(height: 20),
        ElevatedButton(
          onPressed: () async {
            final status = await permissionLocation.status ;
            if (status.isGranted) {
              callNativeMethod();
              print('Opening Location permission...');
            } else {
              // Permission denied
              print('Location permission denied.');
            }

          },
          child: Text('Press Me!'),
        ),
      ],
    ),
  ),
);
  }
  }

Added the permissions in Android Manfiest file: <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

I tried so many way to fix like: Clean the project , Flutter clean, Flutter run, Flutter upgrade but nothing work for me.

Dependencies: environment: sdk: '>=3.2.3 <4.0.0'

 permission_handler: ^11.3.0

any suggestions

1

There are 1 answers

3
Chihiro On

Have you tried stopping the project and then restarting it? Changes involving native-end plugins typically require a project restart to take effect.

I reviewed my project code, and whenever localization is involved, I always use these few. You can try adding them and see.

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

<uses-feature
    android:name="android.hardware.location.network"
    android:required="false" />
<uses-feature
    android:name="android.hardware.location.gps"
    android:required="false" />