Sub topic 11
Why compile-time values are not secrets
This chapter assumes basic Dart and Flutter development, experience with flutter run and flutter build, and familiarity with API base URLs and Flutter project folders.
A compile-time value is a value inserted into an app when Flutter builds it. You can pass one with --dart-define or --dart-define-from-file, then read it with String.fromEnvironment or bool.fromEnvironment.
For example, this command supplies a public API URL:
flutter build apk --dart-define=API_BASE_URL=https://api.example.comDart can read the value while compiling the application:
import 'package:flutter/material.dart';
const apiBaseUrl = String.fromEnvironment(
'API_BASE_URL',
defaultValue: 'https://api.example.com',
);
void main() {
runApp(const MaterialApp(
home: Scaffold(
body: Center(
child: Text('Configured application'),
),
),
));
debugPrint('API base URL: $apiBaseUrl');
}The value is now part of the generated application. It is not fetched only when your server receives a request.
What an attacker can inspect
An Android APK or iOS app bundle must contain enough information to run the application. A person who downloads your app can copy it, inspect its resources, decompile some of its code, or observe its network requests.
Obfuscation can make code harder to read, but it does not turn embedded data into a secret. A URL, feature flag, client identifier, or service configuration can eventually be discovered.
| Value | Usually safe to compile in | Reason |
|---|---|---|
| Public API base URL | Yes | Clients need to connect to it |
| Public feature flag | Yes | It is not an authentication credential |
| OAuth client ID | Often | Some client IDs are designed to be public |
| API key with privileged access | No | Anyone can extract and misuse it |
| Database password | No | It grants direct access to protected data |
A client-side key can still require restrictions. For example, a maps provider may limit a key to known app IDs, signing certificates, domains, or APIs. These restrictions reduce abuse, but the key remains recoverable.
Put privileged operations on a server
The safe pattern is to keep sensitive credentials on a server that you control. The Flutter app authenticates the user and calls your server. Your server then uses its private credential to call another service.
flowchart TD
A[Flutter app] -->|User token and request| B[Your server]
B -->|Private service credential| C[Protected service]
C --> B
B -->|Filtered response| A
D[APK or IPA inspection] -->|Can reveal| AThe app may know the server's public address. It should not know the server's private credential.
A server also gives you a place to validate permissions, rate-limit requests, rotate credentials, and audit use. Do not rely on a hidden value in the app to enforce authorization. Anything shipped to an untrusted device can be extracted or modified.
Configuration is not authorization
Environment-specific configuration remains useful. Dev, staging, and production builds can have different API URLs, logging flags, app names, and public service identifiers. These values select behavior; they do not prove that a request is trusted.
Treat every request from the app as untrusted input. Authenticate it on the server, check the user's permissions there, and validate all submitted data.
When a secret is exposed, revoke or rotate it immediately. Changing the Dart define and rebuilding is not enough if the old app remains installed or the old binary is available for download.
Exercise
You need a production API URL and a production database password. Which value may be passed with --dart-define, and where should the password live?
Show answer
The API URL may be compiled into the app if it is public. The database password must remain on a trusted server and must not be shipped in the Flutter application.