flutter - 如何从 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()
解决方案
您无法从AndroidManifest.xml
文件中获取变量。这是您可以做的,在您的lib
文件夹中创建一个新文件,例如strings.dart
,在该文件中。
把这个写在你新创建的lib/strings.dart
final String apiKey = "your_api_key";
无论您想在哪里使用 API 密钥,只需导入lib/strings.dart
文件,然后像这样使用它:
SearchPlaceWidget(apiKey: apiKey);
推荐阅读
- laravel - Laravel 8:非 SPA 应用程序中的 AJAX 身份验证
- selenium - 如何找到依赖于兄弟类的元素的 xpath
- scala - scala中的split命令不能正常使用特殊字符,如~
- reactjs - 抽屉导航器内的嵌套选项卡导航器问题 React Navigation
- android - Android - WorkManager - Result.success() 调用次数过多
- salesforce - SalesForce SOQL 查找以创建层次结构字符串
- c - 没有明确的类型规范就不能分配给结构
- sql - SQL 每次运行代码时如何添加新列
- python - 我不知道如何使用 Tweepy;你能告诉我吗?
- anylogic - Anylogic,使用不同的时间单位