flutter 权限申请
Posted loaderman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter 权限申请相关的知识,希望对你有一定的参考价值。
添加依赖
permission_handler: ^3.2.2
androidmanifest.xml添加对应的用户权限
在flutter 中app入口申请权限:
import ‘package:flutter/material.dart‘; import ‘package:flutter/services.dart‘; import ‘package:permission_handler/permission_handler.dart‘; import ‘users.dart‘; Future main() async if (!await _requestPermissions()) await AppUtils.popApp(); else runApp(new MyApp()); class MyApp extends StatelessWidget @override Widget build(BuildContext context) final appTitle = "flutter demo"; return new MaterialApp( title: appTitle, home: new JobPage(), ); Future<bool> _requestPermissions() async Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([ PermissionGroup.storage, PermissionGroup.location, ]); List<bool> results = permissions.values.toList().map((status) return status == PermissionStatus.granted; ).toList(); return !results.contains(false); class AppUtils static Future<void> popApp() async await SystemChannels.platform.invokeMethod(‘SystemNavigator.pop‘); static Future checkUpgrade() async try final bool result = await ApplicationMethodChannel.main.invokeMethod(‘checkUpgrade‘); print(‘result=$result‘); on PlatformException print(‘faied‘); class ApplicationMethodChannel static MethodChannel main = MethodChannel(‘main‘);
以上是关于flutter 权限申请的主要内容,如果未能解决你的问题,请参考以下文章
Android权限申请完全解析:Android自带的权限申请
Android 应用开发动态权限管理示例 ( 使用原生代码实现 | 申请权限 | 判定权限申请结果 | 判定 “ 不再询问 “ 情况 )