From 69138acacd0a385cb04f8c1715b82f3cf07d32bc Mon Sep 17 00:00:00 2001 From: fulleni Date: Fri, 24 Oct 2025 19:45:31 +0100 Subject: [PATCH 1/4] refactor(config): enhance AppConfig with environment-aware configurations - Add production() factory constructor with compile-time BASE_URL - Update development() to use localhost or compile-time BASE_URL - Remove unused demo() constructor - Enhance main.dart to support environment-based app initialization --- lib/app/config/app_config.dart | 51 +++++++++++++++++++++++++++------- lib/main.dart | 10 +++++-- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/lib/app/config/app_config.dart b/lib/app/config/app_config.dart index 1c1f793d..56c55f74 100644 --- a/lib/app/config/app_config.dart +++ b/lib/app/config/app_config.dart @@ -1,21 +1,52 @@ + import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/app_environment.dart'; +/// A class to hold all environment-specific configurations. +/// +/// This class is instantiated in `main.dart` based on the compile-time +/// environment variable. It provides a type-safe way to access +/// environment-specific values like API base URLs. class AppConfig { - const AppConfig({required this.environment, required this.baseUrl}); + /// Creates a new [AppConfig]. + AppConfig({ + required this.environment, + required this.baseUrl, + // Add other environment-specific configs here (e.g., analytics keys) + }); - factory AppConfig.production() => const AppConfig( - environment: AppEnvironment.production, - baseUrl: 'http://api.yourproductiondomain.com', - ); + /// A factory constructor for the production environment. + /// + /// Reads the `BASE_URL` from a compile-time variable. Throws an exception + /// if the URL is not provided, ensuring a production build cannot proceed + /// with a missing configuration. This is a critical safety check. + factory AppConfig.production() { + const baseUrl = String.fromEnvironment('BASE_URL'); + if (baseUrl.isEmpty) { + // This check is crucial for production builds. + throw const FormatException( + 'FATAL: The BASE_URL compile-time variable was not provided for this ' + 'production build. Ensure the build command includes ' + '--dart-define=BASE_URL=https://your.api.com', + ); + } + return AppConfig(environment: AppEnvironment.production, baseUrl: baseUrl); + } - factory AppConfig.demo() => - const AppConfig(environment: AppEnvironment.demo, baseUrl: ''); + /// A factory constructor for the demo environment. + factory AppConfig.demo() => AppConfig( + environment: AppEnvironment.demo, + baseUrl: '', // No API access needed for in-memory demo + ); - factory AppConfig.development() => const AppConfig( + /// A factory constructor for the development environment. + factory AppConfig.development() => AppConfig( environment: AppEnvironment.development, - baseUrl: 'http://localhost:8080', + baseUrl: const String.fromEnvironment( + 'BASE_URL', + defaultValue: 'http://localhost:8080', + ), ); final AppEnvironment environment; final String baseUrl; -} +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 340698d1..798864f8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,8 +5,14 @@ import 'package:flutter/material.dart'; import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/config.dart'; import 'package:flutter_news_app_web_dashboard_full_source_code/bootstrap.dart'; -// Define the current application environment (production/development/demo). -const AppEnvironment appEnvironment = AppEnvironment.demo; +// Determine the current application environment from compile-time variables. +// Defaults to 'demo' if no environment is specified. +const AppEnvironment appEnvironment = + String.fromEnvironment('APP_ENVIRONMENT') == 'production' + ? AppEnvironment.production + : (String.fromEnvironment('APP_ENVIRONMENT') == 'development' + ? AppEnvironment.development + : AppEnvironment.demo); @JS('removeSplashFromWeb') external void removeSplashFromWeb(); From aed207a425aac54b998234da9b35f8691272a02a Mon Sep 17 00:00:00 2001 From: fulleni Date: Fri, 24 Oct 2025 19:49:18 +0100 Subject: [PATCH 2/4] docs(README): update environment configuration description - Replace outdated information about environment configuration - Explain production-ready environment tooling using --dart-define - Highlight error-proof configuration and advantages - Improve readability and clarity of the description --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cfb21617..56edb1f0 100644 --- a/README.md +++ b/README.md @@ -107,9 +107,10 @@ Developed with best practices for a maintainable and scalable codebase: --- -### 🛠️ Flexible Environment Configuration -Switch between development (in-memory data or local API) and production environments with a simple code change. -> **🚀 Your Advantage:** This setup speeds up the development cycle and simplifies deployment. +### 🛠️ Production-Ready Environment Tooling +Utilizes compile-time variables (`--dart-define`) to seamlessly switch between `production`, `development`, and `demo` environments. +- **Error-Proof Configuration:** This approach ensures environment-specific settings like API endpoints are set at build time, preventing accidental release of development configurations. +> **Your Advantage:** A robust, professional environment setup that streamlines the development-to-production pipeline and prevents common configuration mistakes. --- From b6b23a0b49349aed3e299593a5969ecb4a2762a5 Mon Sep 17 00:00:00 2001 From: fulleni Date: Fri, 24 Oct 2025 19:49:37 +0100 Subject: [PATCH 3/4] docs: remove changelog file - Removed CHANGELOG.md file as it will be replaced by conventional-changelog - This change allows us to maintain a more dynamic and automated changelog --- CHANGELOG.md | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 894298ad..00000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,9 +0,0 @@ -# Changelog - -## 1.1.0 - 2025-10-17 -- **feat**: allow configuration of saved headlines feed filters limits by user role and update dependencies - - -## 1.0.0 - 2025-10-13 - -- **BREAKING** feat!: migrated from date based versioning to semantic versioning. \ No newline at end of file From 21571ffa585abfc339971e4f84e19ffc1156d92f Mon Sep 17 00:00:00 2001 From: fulleni Date: Fri, 24 Oct 2025 19:51:46 +0100 Subject: [PATCH 4/4] fix(config): replace FormatException with StateError for environment variable check - Replace FormatException with StateError for more appropriate error handling - Remove unnecessary empty line at the beginning of the file - Adjust spacing in the thrown error message for better readability --- lib/app/config/app_config.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/app/config/app_config.dart b/lib/app/config/app_config.dart index 56c55f74..9fd4c666 100644 --- a/lib/app/config/app_config.dart +++ b/lib/app/config/app_config.dart @@ -1,4 +1,3 @@ - import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/app_environment.dart'; /// A class to hold all environment-specific configurations. @@ -23,7 +22,7 @@ class AppConfig { const baseUrl = String.fromEnvironment('BASE_URL'); if (baseUrl.isEmpty) { // This check is crucial for production builds. - throw const FormatException( + throw StateError( 'FATAL: The BASE_URL compile-time variable was not provided for this ' 'production build. Ensure the build command includes ' '--dart-define=BASE_URL=https://your.api.com', @@ -49,4 +48,4 @@ class AppConfig { final AppEnvironment environment; final String baseUrl; -} \ No newline at end of file +}