appConfig static method
app_config.dart — the Flavor enum + AppConfig value object holding
per-flavor settings (app name, API base URL, bundle suffix), plus the
active AppConfig.current set at startup by the entry point.
flavors are the chosen flavor names (already validated). title is the
base app title (Title Case) used to derive each flavor's app name.
Implementation
static String appConfig({
required String title,
required List<String> flavors,
}) {
final enumValues = flavors.join(', ');
String appNameFor(String flavor) =>
flavor == 'prod' ? title : '$title ${_flavorLabel(flavor)}';
String apiBaseUrlFor(String flavor) {
if (flavor == 'prod') return 'https://api.example.com';
final host = flavor.replaceAll('_', '-');
return 'https://$host-api.example.com';
}
String bundleSuffixFor(String flavor) => flavor == 'prod' ? '' : '.$flavor';
final entries = flavors.map((f) => '''
Flavor.$f: AppConfig(
flavor: Flavor.$f,
appName: '${appNameFor(f)}',
apiBaseUrl: '${apiBaseUrlFor(f)}',
bundleSuffix: '${bundleSuffixFor(f)}',
),''').join('\n');
return '''
/// Build flavors for this app.
enum Flavor { $enumValues }
/// Per-flavor configuration. The active config is selected in the flavored
/// entry point (`lib/main_<flavor>.dart`) and assigned to [AppConfig.current]
/// before `runApp`.
class AppConfig {
const AppConfig({
required this.flavor,
required this.appName,
required this.apiBaseUrl,
required this.bundleSuffix,
});
final Flavor flavor;
final String appName;
final String apiBaseUrl;
final String bundleSuffix;
/// The configuration for the running flavor. Set once at startup.
static late AppConfig current;
static const Map<Flavor, AppConfig> _values = {
$entries
};
/// Returns the configuration for [flavor].
static AppConfig of(Flavor flavor) => _values[flavor]!;
}
''';
}