Flutter:E/GoogleMapController(4641):无法启用 MyLocation 层,因为未授予位置权限

Flutter: E/GoogleMapController( 4641): Cannot enable MyLocation layer as location permissions are not granted

我无法显示我移动设备的当前位置。错误是:E/GoogleMapController(4641):无法启用 MyLocation 层,因为未授予位置权限和 E/flutter(4641):[错误:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:清单中未定义位置权限。确保清单中至少定义了 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION。

请看下面的代码。

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class TestScreen extends StatelessWidget {
  static const String idScreen = "testScreen";
  Completer<GoogleMapController> _controller = Completer();
  late GoogleMapController newGoogleMapController;
  static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
  );
  static final CameraPosition _kLake = CameraPosition(
      bearing: 0,
      target: LatLng(14.619256, 121.089854),
      tilt: 0,
      zoom: 19.151926040649414);
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: GoogleMap(
        mapType: MapType.normal,
        initialCameraPosition: _kGooglePlex,
        myLocationEnabled: true,
        zoomGesturesEnabled: true,
        zoomControlsEnabled: true,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: _goToTheLake,
        label: Text('To the lake!'),
        icon: Icon(Icons.directions_boat),
      ),
    );
  }
  late Position currentPosition;
  var geoLocator = Geolocator();
  Future<void> _goToTheLake() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    currentPosition = position;
    LatLng latLngPosition = LatLng(position.latitude, position.longitude);
    CameraPosition cameraPosition = new CameraPosition(
        target: latLngPosition, zoom: 14);
    newGoogleMapController.animateCamera(
        CameraUpdate.newCameraPosition(cameraPosition));
    }
  }

event logs

下面是flutter doctor的结果

J:\Users\Ronel\flutter>flutter doctor -v
[√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 6.1.7601], locale en-US)
    • Flutter version 2.5.3 at J:\Users\Ronel\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (9 weeks ago), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at J:\Users\Ronel\AppData\Local
    • Platform android-31, build-tools 31.0.0
    • ANDROID_HOME = J:\Users\Ronel\AppData\Local
    • Java binary at: J:\Program Files\Android\Android Studio1\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.7)
    • Visual Studio at E:\Program Files\Microsoft Visual Studio19\Community
    • Visual Studio Community 2019 version 16.11.31911.196
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at J:\Program Files\Android\Android Studio1
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] IntelliJ IDEA Community Edition (version 2021.2)
    • IntelliJ at J:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.3
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart

[√] Connected device (3 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 8.0.0 (API 26) (emulator)
    • Windows (desktop)                  • windows       • windows-x64    • Microsoft Windows [Version 6.1.7601]
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 96.0.4664.110

• No issues found!

使用地理定位包中的示例代码解决 (pub.dev)。