Skip to content

Commit 3c03aa3

Browse files
authored
Merge pull request #106 from flutter-news-app-full-source-code/refactor/environment-aware-configurations
refactor(config): enhance AppConfig with environment-aware configurat…
2 parents 59b4e65 + 21571ff commit 3c03aa3

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

CHANGELOG.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ Developed with best practices for a maintainable and scalable codebase:
107107
108108
---
109109

110-
### 🛠️ Flexible Environment Configuration
111-
Switch between development (in-memory data or local API) and production environments with a simple code change.
112-
> **🚀 Your Advantage:** This setup speeds up the development cycle and simplifies deployment.
110+
### 🛠️ Production-Ready Environment Tooling
111+
Utilizes compile-time variables (`--dart-define`) to seamlessly switch between `production`, `development`, and `demo` environments.
112+
- **Error-Proof Configuration:** This approach ensures environment-specific settings like API endpoints are set at build time, preventing accidental release of development configurations.
113+
> **Your Advantage:** A robust, professional environment setup that streamlines the development-to-production pipeline and prevents common configuration mistakes.
113114
114115
---
115116

lib/app/config/app_config.dart

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/app_environment.dart';
22

3+
/// A class to hold all environment-specific configurations.
4+
///
5+
/// This class is instantiated in `main.dart` based on the compile-time
6+
/// environment variable. It provides a type-safe way to access
7+
/// environment-specific values like API base URLs.
38
class AppConfig {
4-
const AppConfig({required this.environment, required this.baseUrl});
9+
/// Creates a new [AppConfig].
10+
AppConfig({
11+
required this.environment,
12+
required this.baseUrl,
13+
// Add other environment-specific configs here (e.g., analytics keys)
14+
});
515

6-
factory AppConfig.production() => const AppConfig(
7-
environment: AppEnvironment.production,
8-
baseUrl: 'http://api.yourproductiondomain.com',
9-
);
16+
/// A factory constructor for the production environment.
17+
///
18+
/// Reads the `BASE_URL` from a compile-time variable. Throws an exception
19+
/// if the URL is not provided, ensuring a production build cannot proceed
20+
/// with a missing configuration. This is a critical safety check.
21+
factory AppConfig.production() {
22+
const baseUrl = String.fromEnvironment('BASE_URL');
23+
if (baseUrl.isEmpty) {
24+
// This check is crucial for production builds.
25+
throw StateError(
26+
'FATAL: The BASE_URL compile-time variable was not provided for this '
27+
'production build. Ensure the build command includes '
28+
'--dart-define=BASE_URL=https://your.api.com',
29+
);
30+
}
31+
return AppConfig(environment: AppEnvironment.production, baseUrl: baseUrl);
32+
}
1033

11-
factory AppConfig.demo() =>
12-
const AppConfig(environment: AppEnvironment.demo, baseUrl: '');
34+
/// A factory constructor for the demo environment.
35+
factory AppConfig.demo() => AppConfig(
36+
environment: AppEnvironment.demo,
37+
baseUrl: '', // No API access needed for in-memory demo
38+
);
1339

14-
factory AppConfig.development() => const AppConfig(
40+
/// A factory constructor for the development environment.
41+
factory AppConfig.development() => AppConfig(
1542
environment: AppEnvironment.development,
16-
baseUrl: 'http://localhost:8080',
43+
baseUrl: const String.fromEnvironment(
44+
'BASE_URL',
45+
defaultValue: 'http://localhost:8080',
46+
),
1747
);
1848

1949
final AppEnvironment environment;

lib/main.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import 'package:flutter/material.dart';
55
import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/config.dart';
66
import 'package:flutter_news_app_web_dashboard_full_source_code/bootstrap.dart';
77

8-
// Define the current application environment (production/development/demo).
9-
const AppEnvironment appEnvironment = AppEnvironment.demo;
8+
// Determine the current application environment from compile-time variables.
9+
// Defaults to 'demo' if no environment is specified.
10+
const AppEnvironment appEnvironment =
11+
String.fromEnvironment('APP_ENVIRONMENT') == 'production'
12+
? AppEnvironment.production
13+
: (String.fromEnvironment('APP_ENVIRONMENT') == 'development'
14+
? AppEnvironment.development
15+
: AppEnvironment.demo);
1016

1117
@JS('removeSplashFromWeb')
1218
external void removeSplashFromWeb();

0 commit comments

Comments
 (0)