Sub topic 4
One entrypoint per environment
This chapter assumes basic Dart and Flutter development, that you can run flutter run and flutter build, and that you know what an API base URL and an app bundle ID are. You will use separate Dart entrypoints to choose configuration before the shared application starts.
Why use separate entrypoints?
An entrypoint is the Dart file where a Flutter application begins, usually a file containing main(). A single app can have several entrypoints:
lib/main_dev.dartfor developmentlib/main_staging.dartfor testinglib/main_prod.dartfor production
Each entrypoint can create the same app widget with different settings. The rest of the UI and business logic stays shared.
This approach makes the selected environment visible in the command that starts the app. It also prevents a developer from changing a production URL in shared application code by mistake.
Create shared app code
Keep the application widget independent from the environment. It receives an AppConfig object rather than deciding which environment is active.
// lib/app.dart
import 'package:flutter/material.dart';
class AppConfig {
const AppConfig({
required this.apiBaseUrl,
required this.enableLogging,
});
final String apiBaseUrl;
final bool enableLogging;
}
class MyApp extends StatelessWidget {
const MyApp({required this.config, super.key});
final AppConfig config;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Environment example',
home: Scaffold(
appBar: AppBar(title: const Text('Environment example')),
body: Center(
child: Text(
'API: ${config.apiBaseUrl}\n'
'Logging: ${config.enableLogging}',
textAlign: TextAlign.center,
),
),
),
);
}
}Add one file for each environment
Each entrypoint imports the shared app and supplies a different constant configuration.
// lib/main_dev.dart
import 'package:flutter/material.dart';
import 'app.dart';
void main() {
runApp(const MyApp(
config: AppConfig(
apiBaseUrl: 'https://api.dev.example.com',
enableLogging: true,
),
));
}// lib/main_prod.dart
import 'package:flutter/material.dart';
import 'app.dart';
void main() {
runApp(const MyApp(
config: AppConfig(
apiBaseUrl: 'https://api.example.com',
enableLogging: false,
),
));
}The files must each contain a main() function. They can use different configuration sources, such as compile-time values supplied with --dart-define. The important boundary is that the entrypoint constructs the configuration and passes it into shared code.
Select an entrypoint with `-t`
The -t option means target. It selects the Dart file that Flutter runs or builds.
flutter run -t lib/main_dev.dart
flutter run -t lib/main_prod.dart
flutter build apk --release -t lib/main_prod.dartWithout -t, Flutter normally uses lib/main.dart. You can keep that file as a default local entrypoint, or make it delegate to a chosen configuration. Be careful when doing so because a default target can hide which environment is running.
The target file and a native flavor are separate choices. The target controls Dart startup code. A flavor controls native build settings such as an Android application ID or an iOS scheme. You may use both options together later:
flutter run --flavor dev -t lib/main_dev.dartExercise
Which part of flutter run --flavor dev -t lib/main_dev.dart selects the Dart entrypoint?
Show answer
-t lib/main_dev.dart selects the Dart entrypoint. --flavor dev selects the native flavor.