热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Flutter权限处理程序无法正常工作

我试图通过请求权限(正确显示对话框)并仅在被授予权限的情况下从我的应用

我试图通过请求权限(正确显示对话框)并仅在被授予权限的情况下从我的应用程序中获取用户的位置。 permission_handler在颤动中使用的库。我无法从设置图块菜单中获取位置小部件以将其打开,并且库中的权限状态显示为permission.disabled

我已经以编程方式打开了我的应用的设置图块
并且它像下面的代码块一样被列出并处于活动状态:

void _requestPerms () async{
Map statuses = await PermissionHandler().requestPermissions([
PermissionGroup.locationWhenInUse,PermissionGroup.locationAlways
]);
final status = statuses[PermissionGroup.locationWhenInUse];
switch(status){
case PermissionStatus.disabled:
await PermissionHandler().openAppSettings();
break;
}
_updateStatus(status);

}

有人可以帮助我理解为什么尽管一次出现对话框提示但电话位置小部件没有打开以及如何在permission granted的情况下导航到下一条路线,或者如何正常退出(如果没有)。谢谢。

完整代码:

import 'package:flutter/material.dart';
import 'package:eme_clone/utils/styles.dart';
import 'package:eme_clone/utils/constants.dart';
import 'package:permission_handler/permission_handler.dart';
class LocationDialog extends StatefulWidget {
@override
_LocationDialogState createState() => _LocationDialogState();
}
class _LocationDialogState extends State {
PermissionStatus _status;
@override
void initState() {
super.initState();
PermissionHandler().checkPermissionStatus(PermissionGroup.locationWhenInUse)
.then(_updateStatus);
}
@override
Widget build(BuildContext context) {
debugPrint('Location Status $_status');
return Scaffold(
body: SafeArea(
child: Column(
children: [
Image.asset('assets/images/perm_icon_location.png'),Text(
locationDialogHeader,style: locationHeaderStyle,),Padding(
padding: const EdgeInsets.all(10.0),child: Text(
locationDialogBody,textAlign: TextAlign.center,style: locationTextBodyStyle,Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,child: Container(
width: double.infinity,padding: EdgeInsets.all(10.0),child: RaisedButton(
color: Colors.black,onpressed: _requestPerms,padding: EdgeInsets.only(top: 15.0,bottom: 15.0),child: Text(
allow,style: allowBtnTextStyle,)
],)),);
}
void _updateStatus(PermissionStatus value) {
setState(() {
_status = value;
});
}
void _requestPerms () async{
Map]);
final status = statuses[PermissionGroup.locationWhenInUse];
switch(status){
case PermissionStatus.disabled:
await PermissionHandler().openAppSettings();
break;
}
_updateStatus(status);
}

}


感谢完整代码。它使调试过程更快。

首先,您需要将以下内容添加到android / app / src / main / AndroidManifest.xml文件中。 (在标签之前)





然后,您可以在等待用户许可的同时使用flutter_spinkit构建初始加载屏幕。
要在用户未授予权限的情况下退出该应用,请使用

SystemChannels.platform.invokeMethod('SystemNavigator.pop');

要在授予请求后导航到下一个屏幕,请签出修改后的完整代码

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LocationDialog(),);
}
}
class LocationDialog extends StatefulWidget {
@override
_LocationDialogState createState() => _LocationDialogState();
}
class _LocationDialogState extends State
with SingleTickerProviderStateMixin {
PermissionStatus _status;
@override
void initState() {
runFirst();
super.initState();
}
runFirst() async {
await PermissionHandler()
.checkPermissionStatus(PermissionGroup.locationWhenInUse)
.then(_updateStatus);
await _requestPerms();
if (_status == PermissionStatus.granted) {
Navigator.push(
context,MaterialPageRoute(builder: (context) => NextPage()));
} else if (_status == PermissionStatus.denied) {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}
}
@override
Widget build(BuildContext context) {
return SpinKitFadingCircle(
itemBuilder: (BuildContext context,int index) {
return DecoratedBox(
decoration: BoxDecoration(
color: index.isEven ? Colors.red : Colors.green,),);
},);
}
void _requestPerms() async {
Map statuses = await PermissionHandler()
.requestPermissions([
PermissionGroup.locationWhenInUse,PermissionGroup.locationAlways
]);
final status = statuses[PermissionGroup.locationWhenInUse];
switch (status) {
case PermissionStatus.disabled:
await PermissionHandler().openAppSettings();
break;
}
_updateStatus(status);
}
void _updateStatus(PermissionStatus value) {
setState(() {
_status = value;
});
}
}
class NextPage extends StatefulWidget {
@override
_NextPageState createState() => _NextPageState();
}
class _NextPageState extends State {
PermissionStatus _status;
@override
Widget build(BuildContext context) {
debugPrint('Location Status $_status');
return Scaffold(
body: SafeArea(
child: Column(
children: [
Image.asset('assets/images/perm_icon_location.png'),Text(
"LocationDialogHelper",Padding(
padding: const EdgeInsets.all(10.0),child: Text(
"locationDialogBody",textAlign: TextAlign.center,Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,child: Container(
width: double.infinity,padding: EdgeInsets.all(10.0),child: RaisedButton(
color: Colors.black,onPressed: _requestPerms,padding: EdgeInsets.only(top: 15.0,bottom: 15.0),child: Text(
"allow",style: TextStyle(color: Colors.white),)
],)),PermissionGroup.locationAlways
]);
final status = statuses[PermissionGroup.locationWhenInUse];
switch (status) {
case PermissionStatus.disabled:
await PermissionHandler().openAppSettings();
break;
}
_updateStatus(status);
}
void _updateStatus(PermissionStatus value) {
setState(() {
_status = value;
});
}
}

推荐阅读
author-avatar
时尚潮_流早覀报_326
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有