After Flutter 3.7 we can store all the API keys inside a JSON file and pass it to a new --dart-define-from-file flag from the command line.
Create a json file with any name ie api-keys.json
{
"API_KEY": "a1b2c33d4e5f6g7h8i9jakblc",
"STRIPE_PUBLISHABLE_KEY": "pk_test_aposdjpa309u2n230ibt23908g"
}
you can get the value against any key as follow
const apiKey = String.fromEnvironment('API_KEY');
if (apiKey.isEmpty) {
throw AssertionError('API_KEY is not set');
}
To run the app with json file run the following command
flutter run --dart-define-from-file=api-keys.json
You can define multiple launch configurations with different API keys if needed ie api-keys.dev.json and api-keys.prod.json.
If you are using VSCode you can add args in the launch.json file as follows
{
"version": "1.0.0",
"configurations": [
{
"name": "Launch",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--dart-define-from-file",
"api-keys.dev.json"
]
}
]
}
If you are using Android Studio you can pass this file as an additional run argument as follow
