Problem
The base URL of the API client in Flutter projects scaffolded with modular_api is typically declared as a Dart constant:
const urlBaseProduccion = 'https://example.com/api';
const urlBaseDesarrollo = 'http://192.168.x.x:4036/api/v2/';
const urlBase = urlBaseDesarrollo; // change to urlBaseProduccion for production
Switching environments requires editing source code, recompiling, and redeploying. This is error-prone: it is easy to ship a production build pointing at the development server, or vice versa. The comment is a manual step that will eventually be missed.
Impact
- Risk of accidental production builds pointing at development infrastructure
- CI/CD pipelines cannot build environment-specific artifacts without source modifications
- Violates the 12-factor app principle of separating config from code
Proposed solution
Replace the hardcoded constant with an environment-driven mechanism appropriate for Flutter web and mobile:
- Flutter --dart-define build flags — pass
BASE_URL at build time via flutter build web --dart-define=BASE_URL=https://... and read it with const String.fromEnvironment('BASE_URL'). Zero runtime overhead, works for web and mobile, CI-friendly.
- flutter_dotenv or equivalent — read from a .env file at runtime. More flexible but adds a dependency and a file that must be deployed alongside the app.
Option 1 (--dart-define) is the recommended approach for Flutter web: it bakes the URL into the compiled JS at build time with no extra package required.
The macss CLI scaffolding for new Flutter projects should generate this pattern by default instead of a hardcoded constant.
Related
- Affects: Flutter SDK client scaffolding in modular_api
- The macss CLI project template should be updated alongside this fix
Problem
The base URL of the API client in Flutter projects scaffolded with modular_api is typically declared as a Dart constant:
Switching environments requires editing source code, recompiling, and redeploying. This is error-prone: it is easy to ship a production build pointing at the development server, or vice versa. The comment is a manual step that will eventually be missed.
Impact
Proposed solution
Replace the hardcoded constant with an environment-driven mechanism appropriate for Flutter web and mobile:
BASE_URLat build time viaflutter build web --dart-define=BASE_URL=https://...and read it withconst String.fromEnvironment('BASE_URL'). Zero runtime overhead, works for web and mobile, CI-friendly.Option 1 (
--dart-define) is the recommended approach for Flutter web: it bakes the URL into the compiled JS at build time with no extra package required.The macss CLI scaffolding for new Flutter projects should generate this pattern by default instead of a hardcoded constant.
Related