在 Flutter 应用程序中使用开源 GitHub 存储库中的谷歌地图 api 密钥

问题描述 投票:0回答:1

我正在通过 Flutter 开发一个开源 Google 地图项目,当我与 Google 地图集成时,我不希望 GMS API 出现在我的存储库中。我不知道如何在 AppDelegate.swift 和 AndroidManifest.xml 文件中使用 .env 文件中的变量。

我的AndroidManifest.XML

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

AppDelegate.swift

import UIKit
import Flutter
import GoogleMaps

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GMSServices.provideAPIKey("MY_API_KEY")
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

我在路径中添加了一个 .env 文件,但在 .gitignore 上忽略了它。使用这种方法,我可以通过 flutter_dotenv 包轻松地在 lib 文件夹的文件中使用它。

flutter dart google-maps google-maps-android-api-2 api-key
1个回答
0
投票

对于安卓你可以尝试这个:

  1. 创建一个值
    xml
    文件
    keys.xml
    在此文件中添加您的密钥,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="google_map_api_key">Your Api Key</string>
</resources>
  1. AndroidManifest.xml
    中使用它,如下所示:
     <meta-data
       android:name="com.google.android.geo.API_KEY"
       android:value="@string/google_map_api_key" />

并将

keys.xml
添加到
.gitignore
文件以忽略 git 存储库中对该文件的跟踪。

将其添加到 .gitignore 文件中

# ignore keys.xml
/android/app/src/main/res/values/keys.xml
© www.soinside.com 2019 - 2024. All rights reserved.