Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将Google Maps添加到Flatter时未找到API键_Java_Android_Google Maps_Flutter - Fatal编程技术网

Java 将Google Maps添加到Flatter时未找到API键

Java 将Google Maps添加到Flatter时未找到API键,java,android,google-maps,flutter,Java,Android,Google Maps,Flutter,尝试将google maps api添加到Flatter时,我在日志中发现以下错误: /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 th

尝试将google maps api添加到Flatter时,我在日志中发现以下错误:

/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
/MethodChannel#flatter/platform_视图(11205):java.lang.RuntimeException:未找到API键。检查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) {},
          ),
        ),
      ],
    );
  }
}

我试图在其中显示地图的文件如下所示:

<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) {},
          ),
        ),
      ],
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“reservation.dart”;
导入“dart:async”;
导入“包:cloud_firestore/cloud_firestore.dart”;
导入“包:google_-maps_-flatter/google_-maps_-flatter.dart”;
导入“包:flifter/services.dart”;
导入“feed.dart”;
类信息扩展了无状态小部件{
信息([this.id]);
int-id;
@凌驾
小部件构建(构建上下文){
返回脚手架(
floatingActionButton:floatingActionButton.extended(
已按下:(){
导航器。推(
上下文
MaterialPage路线(生成器:(上下文)=>保留(id)),
);
},
图标:图标(今天的图标、日历),
标签:文本(“预订表格”),
背景颜色:颜色。蓝灰色,
),
正文:_buildBody(上下文),
);
}
Widget\u buildBody(BuildContext上下文){
返回流生成器(
流:Firestore.instance
.收藏(‘餐厅’)
.其中('id',isEqualTo:id)
.snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)返回LinearProgressIndicator();
返回构建列表(上下文、快照、数据、文档);
},
);
}
小部件构建列表(构建上下文上下文,列表快照){
返回列表视图(
物理:弹跳CrollPhysics(),
收缩膜:对,
儿童:[
新容器(
装饰:新的BoxEdition(boxShadow:[
新盒影(
颜色:颜色。蓝灰色,
半径:20.0,
)
]),
子项:新映像。网络(快照[0]。数据['picURL']),
),
填充物(
填充:常数边集全部(7.0),
子项:文本(快照[0]。数据['name'],
样式:TextStyle(fontWeight:fontWeight.bold,fontSize:36)),
),
填充物(
填充:常数边集全部(7.0),
子项:文本(快照[0]。数据['desc'],
样式:TextStyle(fontWeight:fontWeight.bold,fontSize:36)),
),
容器(
高度:MediaQuery
.of(上下文)
.尺寸
.身高,
宽度:MediaQuery
.of(上下文)
.尺寸
.宽度,
孩子:谷歌地图(
初始CameraPosition:CameraPosition(目标:LatLng(37.4219999,-122.0862462)),
onMapCreated:(GoogleMapController){},
),
),
],
);
}
}
我多次尝试重新生成密钥并重新启动我的应用程序和android studio,但都无济于事


谢谢

您目前已将
元数据
元素作为活动的一部分。文档中说,要使其成为应用程序的子项:

在AndroidManifest.xml中,将以下元素作为
元素的子元素添加,方法是将其插入到关闭标记
之前


因此,我建议您尝试将其移动到

之后,如果您首先将
标记放置在错误的位置,让我们说一下在
标记中。

  • 您需要运行清理android和ios目录的“flutter clean”

  • 然后确保您在
    标记的正上方创建了
    标记


  • 你该走了。我遇到了这个问题,它对我起了作用。找到了github问题的答案

    在我的例子中,我操作了一个错误的清单文件,因为有两个同名文件,一个在src目录中,另一个在src/main中,请确保使用后一个,LOL。

    这里有一些快速修复方法,您可以尝试一下,并稍微清除上面的答案:

  • 运行
    flatter clean

  • 将密钥添加到
    AndroidManifest.xml
    android/app/src/main):

    尝试将右键放在应用程序元素之后

    打开
    android
    app
    src
    main
    --AndroidManifest.xml,将Google Maps元数据标签粘贴到活动标签之前的应用程序标签中,将之前复制的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密钥


  • 我也这么做了。但仍然面临未找到的APIissue@SandeepMaurya:我建议您使用确切的XML和确切的错误消息提出一个新问题。(错误是“未找到API”还是“未找到API密钥”?它们非常不同!)这并不能真正回答问题。如果您有不同的问题,可以单击以提问。一旦你有足够的时间,你也可以吸引更多的注意力-