我在打包升级“定位服务”时遇到了这个问题

时间:2020-03-17 21:39:47

标签: flutter dart

升级软件包时出现此错误 “ lib / location_helper.dart:43:23:错误:无法将类型'PermissionStatus'的值分配给'bool'类型的变量。 -“ PermissionStatus”来自“ package:location / location.dart”(“ / D:/flutter/.pub-cache/hosted/pub.dartlang.org/location-2.5.3/lib/location.dart”)。 _permission =等待_locationService.requestPermission(); ^ 目标kernel_snapshot失败:异常:创建快照时出错:null 构建失败。 “

location_helper

import 'package:location/location.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'dart:async';
import 'package:flutter/services.dart';

class LocationHelper {
  ///init as singleton
  static LocationHelper _locationHelper;

  static LocationHelper instance() {
    if (_locationHelper == null) {
      _locationHelper = LocationHelper._();
    }
    return _locationHelper;
  }

  LocationHelper._();

  LocationData _startLocation;
  LocationData _currentLocation;
  StreamSubscription<LocationData> _locationSubscription;
  Location _locationService = new Location();
  bool _permission = false;
  String error;
  Completer<GoogleMapController> _controller = Completer();
  static final CameraPosition _initialCamera = CameraPosition(
    target: LatLng(0, 0),
    zoom: 4,
  );

  CameraPosition _currentCameraPosition;

  Future<void> _initPlatformState() async {
    await _locationService.changeSettings(
        accuracy: LocationAccuracy.HIGH, interval: 1000);

    LocationData location;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      bool serviceStatus = await _locationService.serviceEnabled();
      print("Service status: $serviceStatus");
      if (serviceStatus) {
        _permission = await _locationService.requestPermission();
        print("Permission: $_permission");
        if (_permission) {
          location = await _locationService.getLocation();

          _locationSubscription = _locationService
              .onLocationChanged()
              .listen((LocationData result) async {
            _currentCameraPosition = CameraPosition(
                target: LatLng(result.latitude, result.longitude), zoom: 16);

            final GoogleMapController controller = await _controller.future;
            controller.animateCamera(
                CameraUpdate.newCameraPosition(_currentCameraPosition));

            _currentLocation = result;
          });
        }
      } else {
        bool serviceStatusResult = await _locationService.requestService();
        print("Service status activated after request: $serviceStatusResult");
        if (serviceStatusResult) {
          _initPlatformState();
        }
      }
    } on PlatformException catch (e) {
      print(e);
      if (e.code == 'PERMISSION_DENIED') {
        error = e.message;
      } else if (e.code == 'SERVICE_STATUS_ERROR') {
        error = e.message;
      }
      location = null;
    }

    _startLocation = location;
  }

  Future<LocationData> getCoordinates() async {
    await _initPlatformState();

    return _startLocation;
  }

  Future<String> getCoordinatesAsString() async {
    await _initPlatformState();

    return '${_startLocation.latitude}' + ',' + '${_startLocation.longitude}';
  }
}

1 个答案:

答案 0 :(得分:0)

我认为问题是您的“ bool _permission = false”。和布尔一起做是错误的。 我查看了位置包,您应该像这样检查权限;

PermissionStatus _permission;

_permission = await location.hasPermission();
if (_permission == PermissionStatus.DENIED) {
  _permission = await location.requestPermission();
  if (_permission != PermissionStatus.GRANTED) {
    return;
  }
} // Then do you codes.