禁用设备位置后,启动时flutter应用程序崩溃

我有一个带定位功能的轻量级应用程序,它可以正常工作,但是有一个无法修复的错误,当我启动该应用程序并且location / gps已关闭时触发,它要求获得许可,然后使该应用程序崩溃。

这是我使用的代码:

import 'package:location/location.dart' as loc;



  Future<loc.LocationData> getLocation() async {
    try {
      loc.Location location = loc.Location();
      bool _serviceEnabled;
      loc.PermissionStatus _permissionGranted;

      _serviceEnabled = await location.serviceEnabled();
      if (!_serviceEnabled) {
        _serviceEnabled = await location.requestService();
        if (!_serviceEnabled) {
          return null;
        }
      }

      _permissionGranted = await location.hasPermission();
      if (_permissionGranted == loc.PermissionStatus.denied) {
        _permissionGranted = await location.requestPermission();
        if (_permissionGranted != loc.PermissionStatus.granted) {
          return null;
        }
      }
      return await location.getLocation();
    } catch (e) {
      print(e);
    }
  }