我不知道如何解决这个红色错误的一部分。我从互联网上复制这个,然后我得到了这个错误。请帮助我解决这个问题。
这是我的完整代码。
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:android_intent/android_intent.dart';
import 'package:geolocator/geolocator.dart';
class AskForPermission extends StatefulWidget {
@override
_AskForPermissionState createState() => _AskForPermissionState();
}
class _AskForPermissionState extends State<AskForPermission> {
final PermissionHandler permissionHandler = PermissionHandler();
Map<PermissionGroup, PermissionStatus>? permissions;
void initState() {
super.initState();
requestLocationPermission();
_gpsService();
}
Future<bool> _requestPermission(PermissionGroup permission) async {
final PermissionHandler _permissionHandler = PermissionHandler();
var result = await _permissionHandler.requestPermissions([permission]);
if (result[permission] == PermissionStatus.granted) {
return true;
}
return false;
}
/*Checking if your App has been Given Permission*/
Future<bool> requestLocationPermission({Function? onPermissionDenied}) async {
var granted = await _requestPermission(PermissionGroup.location);
if (granted!=true) {
requestLocationPermission();
}
debugPrint('requestContactsPermission $granted');
return granted;
}
/*Show dialog if GPS not enabled and open settings location*/
Future _checkGps() async {
if (!(await Geolocator.isLocationServiceEnabled())) {
if (Theme.of(context).platform == TargetPlatform.android) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Can't get gurrent location"),
content:const Text('Please make sure you enable GPS and try again'),
actions: <Widget>[
FlatButton(child: Text('Ok'),
onPressed: () {
final AndroidIntent intent = AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS');
intent.launch();
Navigator.of(context, rootNavigator: true).pop();
_gpsService();
})],
);
});
}
}
}
/*Check if gps service is enabled or not*/
Future _gpsService() async {
if (!(await Geolocator.isLocationServiceEnabled())) {
_checkGps();
return null;
} else
return true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Ask for permisions'),
backgroundColor: Colors.red,
),
body: Center(
child: Column(
children: <Widget>[
Text("All Permission Granted"),
],
))
);
}
}
5条答案
按热度按时间xtfmy6hx1#
我也遇到了同样的问题--在我升级到^8.3.0后,与permission_handler ^8.0.0+2一起工作的代码不再工作了--代码无法编译,Permission和其他类不再“找到”(我使用VS Code)
我通过清理pub缓存修复了它:
然后再次得到依赖关系:
PS:为了升级到8.3.0,我在build.gradle文件中设置了targetSdkVersion和compileSdkVersion为31
gywdnpxw2#
安装权限处理程序包from here。然后在.dart文件中添加import并在某处调用权限请求函数。例如,像这样的函数要求存储权限。
mefy6pfw3#
请参考下面的代码
https://pub.dev/packages/permission_handler/install permission_handler:^5.0.1+1
cyvaqqii4#
在
pubspec.yaml
文件中添加permission_handler
依赖项。尝试
flutter pub get
命令。如果已经完成,请参考有关permission handler package的链接。7cjasjjr5#
import 'package:permission_handler/permission_handler.dart' as ph;
...
int n = int n. int n();
...