'PermissionGroup'不能分配给参数类型'List <PermissionGroup>'Flutter

时间:2019-10-07 07:33:20

标签: flutter dart permissions

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Permissions",
      home: PermissionPage(),
    );
  }
}

class PermissionPage extends StatefulWidget {
  @override
  _PermissionPageState createState() => _PermissionPageState();
}

class _PermissionPageState extends State<PermissionPage> {
  PermissionStatus _status;

  @override
  void initState() {
    super.initState();

    PermissionHandler()
        .checkPermissionStatus(PermissionGroup.locationWhenInUse)
        .then(_updatePermissionStatus);
  }

  void _requestPermission() {
    PermissionHandler()
        .requestPermissions(PermissionGroup.locationWhenInUse)
        .then(_onStatusRequested);
  }

  void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
    final status = statuses[PermissionGroup.locationWhenInUse];
    _updatePermissionStatus(status);
  }

  void _updatePermissionStatus(PermissionStatus status) {
    if (status != _status) {
      setState(() {
        _status = status;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Permissions"),
      ),
      body: Center(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Permission Status -> $_status"),
              SizedBox(
                height: 50.0,
              ),
              RaisedButton(
                child: Text("Request Location Permission"),
                onPressed: _requestPermission,
              )
            ],
          ),
        ),
      ),
    );
  }
}

void _requestPermission() {
PermissionHandler()
    .requestPermissions(PermissionGroup.locationWhenInUse)
    .then(_onStatusRequested);

}

在上述方法中,显示了以下给定错误

  

不能将参数类型“ PermissionGroup”分配给参数类型“ List”。

我是Flutter的新手,就在一周前,我开始了Flutter。 请朋友帮我解决这个错误。 或者如果还有其他更有效的方式来询问用户权限,请也告诉我。

0 个答案:

没有答案