码迷,mamicode.com
首页 > 其他好文 > 详细

flutter 权限申请

时间:2019-09-19 21:19:25      阅读:581      评论:0      收藏:0      [点我收藏+]

标签:and   try   except   end   val   manifest   static   uil   location   

添加依赖

  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 权限申请

标签:and   try   except   end   val   manifest   static   uil   location   

原文地址:https://www.cnblogs.com/loaderman/p/11532340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!