location.dart 750B

12345678910111213141516171819202122232425
  1. import 'package:location/location.dart';
  2. Future requireLocation() async {
  3. Location location = Location();
  4. bool _serviceEnabled;
  5. PermissionStatus _permissionGranted;
  6. _serviceEnabled = await location.serviceEnabled();
  7. if (!_serviceEnabled) {
  8. _serviceEnabled = await location.requestService();
  9. if (!_serviceEnabled) {
  10. throw Exception("请开启系统定位功能");
  11. }
  12. }
  13. _permissionGranted = await location.hasPermission();
  14. if (_permissionGranted == PermissionStatus.denied) {
  15. _permissionGranted = await location.requestPermission();
  16. if (_permissionGranted != PermissionStatus.granted) {
  17. throw Exception("请设置允许当前程序定位功能");
  18. }
  19. }
  20. return location;
  21. }