首页 > 解决方案 > 如何从 Flutter 中的 Android Manifest 获取变量?

问题描述

我想在我的包中使用 Google Maps API 的 API 密钥,假设它存在于 AndroidManifest 文件(和 iOS 等效文件)中。我看到如果你使用这个google_maps_flutter包,你永远不必将 API 密钥传递给GoogleMap小部件。目前,我将 API 密钥作为属性传递给我使用的每个小部件,如下所示:

// The class receives the API key through a property called apiKey SearchPlaceWidget(apiKey: YOUR_API_KEY);

我想要这样的东西:

// The class will get the API key directly from the AndroidManifest file SeachPlaceWidget()

标签: flutterdart

解决方案


您无法从AndroidManifest.xml文件中获取变量。这是您可以做的,在您的lib文件夹中创建一个新文件,例如strings.dart,在该文件中。

把这个写在你新创建的lib/strings.dart

final String apiKey = "your_api_key";

无论您想在哪里使用 API 密钥,只需导入lib/strings.dart文件,然后像这样使用它:

SearchPlaceWidget(apiKey: apiKey);

推荐阅读