将 Google 地图添加到 Flutter 时找不到 API 密钥

2022-09-03 08:27:52

当尝试将谷歌地图API添加到Flutter时,我在日志中收到以下错误:

/MethodChannel#flutter/platform_views(11205): java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

我的 AndroidManifest.xml 文件如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foody.foody">


<!-- The INTERNET permission is required for development. Specifically,
     flutter needs it to communicate with the running application
     to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
     calls FlutterMain.startInitialization(this); in its onCreate method.
     In most cases you can leave this as-is, but you if you want to provide
     additional functionality it is fine to subclass or reimplement
     FlutterApplication and put your custom class here. -->
<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="foody"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <!-- This keeps the window background of the activity showing
             until Flutter renders its first frame. It can be removed if
             there is no splash screen (such as the default splash screen
             defined in @style/LaunchTheme). -->
        <meta-data
            android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
            android:value="true" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyBM8ywYw1UDb3aXaTF3w21EJ86ePWmAkPE"/>

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

我尝试显示地图的文件如下所示:

import 'package:flutter/material.dart';
import 'reservation.dart';
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter/services.dart';
import 'feed.dart';

class info extends StatelessWidget {
  info([this.id]);

  int id;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => Reservation(id)),
          );
        },
        icon: Icon(Icons.calendar_today),
        label: Text('Book a Table'),
        backgroundColor: Colors.blueGrey,
      ),
      body: _buildBody(context),
    );
  }

  Widget _buildBody(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance
          .collection('restaurants')
          .where('id', isEqualTo: id)
          .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return LinearProgressIndicator();

        return _buildList(context, snapshot.data.documents);
      },
    );
  }

  Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
    return ListView(
      physics: BouncingScrollPhysics(),
      shrinkWrap: true,
      children: <Widget>[
        new Container(
          decoration: new BoxDecoration(boxShadow: [
            new BoxShadow(
              color: Colors.blueGrey,
              blurRadius: 20.0,
            )
          ]),
          child: new Image.network(snapshot[0].data['picURL']),
        ),
        Padding(
          padding: const EdgeInsets.all(7.0),
          child: Text(snapshot[0].data['name'],
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 36)),
        ),
        Padding(
          padding: const EdgeInsets.all(7.0),
          child: Text(snapshot[0].data['desc'],
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 36)),
        ),
        Container(
          height: MediaQuery
              .of(context)
              .size
              .height,
          width: MediaQuery
              .of(context)
              .size
              .width,
          child: GoogleMap(
            initialCameraPosition: CameraPosition(target: LatLng(37.4219999, -122.0862462)),
            onMapCreated: (GoogleMapController controller) {},
          ),
        ),
      ],
    );
  }
}

我尝试重新生成密钥并多次重新启动我的应用程序和Android工作室,但无济于事。

谢谢


答案 1

您当前已将该元素作为活动的一部分。文档说要使其成为应用程序的子级meta-data

在 AndroidManifest.xml中,通过将以下元素插入到结束标记之前,将以下元素添加为元素的子元素。<application></application>

所以我建议你试着把它移到之后。</activity>


答案 2

如果您一开始将标签放在错误的位置,请说在标签中<meta-data><activity>

  1. 您需要运行“flutter clean”来清理android和ios构建目录。

  2. 然后,确保在标记的正上方创建标记。<meta-data></application>

你应该很好去。我遇到了这个问题,它对我有用。在github问题上找到了答案。


推荐