簡體   English   中英

將 Google Maps 添加到 Flutter 時找不到 API Key

[英]API Key not found when adding Google Maps to Flutter

嘗試將 google maps 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 studio,但無濟於事。

謝謝

您目前已將meta-data元素作為活動的一部分。 文檔說要使它成為應用程序的子項:

在 AndroidManifest.xml 中,添加以下元素作為<application>元素的子元素,方法是將其插入到</application>標記之前

所以我建議你試着把它移到</activity>

如果您一開始將<meta-data>標簽放在錯誤的位置,讓我們說在<activity>標簽中。

  1. 您需要運行“flutter clean”來清理 android 和 ios 目錄。

  2. 然后確保在</application>標記正上方創建<meta-data> </application>標記。

你應該很高興去。 我遇到了這個問題,它對我有用。 在github問題上找到了答案。

就我而言,我操作了一個錯誤的清單文件,因為有 2 個同名文件,一個在 src 目錄中,另一個在 src/main 中,請確保使用后一個,大聲笑。

這是您可以嘗試的一些快速修復方法,並稍微清除上述答案:

  1. 運行flutter clean

  2. 將密鑰添加到AndroidManifest.xml ( android/app/src/main ):

    嘗試將右側放在應用程序元素之后。

    打開android > app > src > main ‣ AndroidManifest.xml 並將谷歌地圖元數據標簽粘貼到您的應用程序標簽中的活動標簽之前,將您之前復制的 API 密鑰替換為{{YOUR_API_KEY_HERE}}

     <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.succo.succo"> … <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application> <meta-data android:name="com.google.android.geo.API_KEY" android:value="{{YOUR_API_KEY_HERE}}"/> </application> </manifest>

    對於值字段,請粘貼您在 Google Maps API 注冊過程中獲得的 API 密鑰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM