flutter 使用permission_handler授权

2022-06-23  本文已影响0人  职场过客

添加依赖:^8.1.6

permission_handler: any

具体实现:

  _requestPermission() async {
    var status = await Permission.camera.status;
    var status1 = await Permission.storage.status;
    var status2 = await Permission.photos.status;
    if (status1 != PermissionStatus.granted ||
        status != PermissionStatus.granted ||
        status2 != PermissionStatus.granted) {
      var future = await [
      Permission.camera,
      Permission.storage,
      Permission.photos,
    ].request();

      for (final item in future.entries) {
        if (item.value != PermissionStatus.granted) {
          return false;
        }
      }
    }
    return true;
  }

尽可能在initState调用

 @override
  void initState() {
    super.initState();
    flutterChannel.setMethodCallHandler(handler);
    _requestPermission();
  }
上一篇下一篇

猜你喜欢

热点阅读