12345678910111213141516171819202122232425 |
- import 'package:location/location.dart';
-
- Future requireLocation() async {
- Location location = Location();
- bool _serviceEnabled;
- PermissionStatus _permissionGranted;
-
- _serviceEnabled = await location.serviceEnabled();
- if (!_serviceEnabled) {
- _serviceEnabled = await location.requestService();
- if (!_serviceEnabled) {
- throw Exception("请开启系统定位功能");
- }
- }
-
- _permissionGranted = await location.hasPermission();
- if (_permissionGranted == PermissionStatus.denied) {
- _permissionGranted = await location.requestPermission();
- if (_permissionGranted != PermissionStatus.granted) {
- throw Exception("请设置允许当前程序定位功能");
- }
- }
-
- return location;
- }
|