我已经为位置权限创建了一个演示应用程序,但在使用permission_handler_11.3.0时我发现了MissingPluginException的问题。
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!'),
),
],
),
),
);
}
}
在Android Manfiest文件中添加了权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我尝试了很多方法来修复,例如:清理项目、Flutter 清理、Flutter 运行、Flutter 升级,但对我来说没有任何作用。
依赖关系:
environment: sdk: '>=3.2.3 <4.0.0'
permission_handler: ^11.3.0
有什么建议吗
您是否尝试过停止该项目然后重新启动它?涉及本机端插件的更改通常需要重新启动项目才能生效。