A utility to help retrieve the native flavor running and changing settings based on this.
Getting only the running flavor name:
final String? flavorName = await FlutterNativeFlavors.getFlavorName();
Creating different configs and retrieving them based on running flavor:
// AppConfig is totally up to you how to implement and how many configs you wanna have
class AppConfig {
final String flavor;
final String apiUrl;
AppConfig({
required this.flavor,
required this.apiUrl,
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final flavors = FlavorConfig({
'prod': AppConfig(
flavor: 'prod',
apiUrl: 'https://production.api.example.com',
),
'beta': AppConfig(
flavor: 'beta',
apiUrl: 'https://beta.api.example.com',
),
});
// When you're implementing this plugin, it's reccomended not to use a default option to better detect configuration errors
final AppConfig? config = await flavors.getRunningFlavorConfig(defaultFlavor: 'prod');
runApp(MyApp(
config: config!, // To avoid crashes, only force with "!" if you provided a valid default option above
));
}
- Open your Flutter project in Xcode by navigating to
ios/Runner.xcworkspace
. - In Xcode, select your application target (Runner) and go to the Build Settings tab.
- Under the User-Defined section, add a new build setting called
APP_FLAVOR
and set it to the desired flavor value for each scheme. - Open the
Info.plist
file located in theios/Runner
directory. - Add a new entry to the Information Property List with the key
App - Flavor
and the value$(APP_FLAVOR)
. This will ensure that the flavor value is accessible in the Info.plist file. - Save the
Info.plist
file. - Build and run your application using the desired scheme and flavor.
- Open your Flutter project in Android Studio.
- Locate the
android/app/build.gradle
file. - Inside the
android
block, ensure that thedefaultConfig
section contains the following line:
android {
namespace "com.example.myapp" // Replace with your namespace
// ...
defaultConfig {
// ...
applicationId "com.example.myapp" // Replace with your applicationId; make sure it's similar to namespace
// ...
}
}
The applicationId should match the package name defined in your AndroidManifest.xml file. 4. To add flavors, add the following lines to the android block:
flavorDimensions "default"
productFlavors {
beta {
applicationIdSuffix ".beta" // com.example.myapp.beta
}
prod {
applicationIdSuffix ".prod" // com.example.myapp.prod
}
}
Customize the flavor names (dev and prod) and the applicationIdSuffixes (.dev and .prod) to match your desired flavors.
After retrieving your config object, you can use anything you already have to provide dependencies to your app (dependency injection, provider, get_it, etc.).
The package includes flutter default inhereted widget shorthand functions, if you want to use it.
Setting up:
// Wrapping MaterialApp with this will provide the config for the entire app
return FlavorConfig.inject<AppConfig>(
config: appConfig,
child: MaterialApp(
home: ///...
),
);
Retrieving:
// On AppConfig class, add this:
class AppConfig {
// ...
static AppConfig of(BuildContext context) => FlavorConfig.getFromContext(context)!
}
// Then on any widget you can use:
AppConfig.of(context)