Imagine if you could mold and modify your main app codebase into distinct variations without altering its essence. That’s precisely where “Flutter flavors” come into play. 

Whether you’re tailoring your Flutter app for different environments, creating demo versions, or rebranding entirely, understanding how to utilize flavors effectively can significantly optimize your development process. In this guide, we’ll dive deep into Flutter flavors, why they’re indispensable, and how you can implement them for Android and iOS.

What are flavors in Flutter?

Let’s use a culinary analogy to understand the concept of flavors in Flutter.

Imagine you’re in a kitchen, preparing a meal with chicken as the primary ingredient. Chicken, a versatile protein, can be the foundation for many dishes, each offering a unique taste and presentation. Similarly, in the Flutter project, the main codebase is analogous to our chicken. It’s the fundamental part of our Flutter application, but how we serve or present it can vary depending on our needs.

Chicken can be prepared in various ways or various flavors, for example:

  • Sauté,
  • Breaded,
  • Boiled,
  • Sweet & Chilli,
  • Curry.

By defining different flavors in Flutter, developers can use the same foundational codebase (our chicken) and modify configurations, settings, and features (our ingredients and cooking methods) to produce distinct versions of the application.

Why should you use flavors in your Flutter app? 

Flavors are a technique to create various application variants using a single codebase. It allows us to define custom arguments and parameters during compilation time, which affects how the application behaves.

The image displays a section of a build.gradle file. The content is focused on defining Android app build flavors based on different environments. There are two flavor dimensions specified, named "environment". Two product flavors are defined: qa and prod.

During mobile app development, we often want to work in different environments. To do so, Flavors come in handy.

While developing new features, especially in large production apps, we’d instead use our development environment not to interfere with existing production users or to avoid application downtime.

Using flavors to build applications with specific environments is also helpful during bug fixing, where QA engineers can set up the application in one particular way to reproduce production issues that have been reported. In contrast, in a production environment, it could be very hard or even impossible to make it in a way that does not impact the users.

This is just one use case of flavors. Using this technique, we can also differentiate our application. 

Flavors will also be helpful to create:

  • Free demo version of the paid application;
  • Another, basically the same application with another branding;
  • When it is the same application but with a different name or icon.

How do you set up flavors in the Flutter for iOS?

1. First, we need to create new product schemes in XCode. In our example, it is prod and qa. 

In XCode, select Product -> Scheme -> New scheme.

A screenshot of the Xcode IDE displaying the project structure on the left, with files related to a Flutter application. On the center-right, there are project settings and configurations, including options for building, running, and managing schemes. The top of the window shows an iPhone 14 Pro Max as the selected simulator and a file path indicating it's related to a Flutter demo.

2. Once schemes are created, we have to duplicate build configurations: 

Debug, Release, and Profile once per the scheme, with the copied build configuration name + prefix as a name. 

As a prefix, use the current scheme name. 

A screenshot of the Xcode IDE focused on a Flutter project named "Runner". On the left, there's a project hierarchy with various files and folders. The central portion showcases the "Info" tab, detailing settings such as iOS Deployment Target, Configurations, and Localizations. The top right section provides information about the project's identity, location, and text settings.

3. Now, we need to update our previously created schemes with the correct build configuration.

In XCode, select Product -> Scheme -> Manage schemes

A screenshot of the Xcode IDE displaying a Flutter project named "Runner". On the left, there's a project hierarchy. In the center, a dropdown menu titled "prod" lists various build and run configurations, including "Debug-prod" and "Release-prod". On the right, settings under the "Info" tab are shown, detailing the build configuration and other launch settings. The bottom of the centered dropdown has options to duplicate the scheme or manage schemes.

At this point, the basic configuration is ready, and we can start differentiating our application, for example:

  • Set application name
A screenshot of the Xcode IDE showcasing the "Runner" Flutter project. On the left, there's a directory structure including folders like "Flutter" and "Runner". The central panel displays project and target settings with a focus on "Packaging", indicating various build flavors like "Debug-qa" and "Release-prod". On the right, detailed "Identity and Type" settings for the "Runner" project are presented, including its location and project format. The top bar indicates the current build configuration is set to "qa" for "iPhone 14 Pro".
  • Set different bundle id
A screenshot of the Xcode IDE showing a Flutter project named "Runner". On the left, there's a project hierarchy with various files and folders. The central portion displays the "Signing & Capabilities" tab for the project, indicating settings for app signing, including team details and bundle identifier. The top right displays project location details and text settings.

How do you set up flavors in the Flutter for Android?

In the case of Android, setup is a bit simpler because we don’t have to deal with the XCode.

  1. First, find your build.gradle file (make sure its app build.gradle)
  2.  In build.gradle add flavor configuration.
android > app > build.gradle

We can set a bunch of options like applicationId, applicationIdSuffix, resource values like app name, custom strings with api host, etc.

The image displays a section of a build.gradle file. The content is focused on defining Android app build flavors based on different environments. There are two flavor dimensions specified, named "environment". Two product flavors are defined: qa and prod.

This is just a basic Flavors configuration. You can read more on

How do you run Flutter flavor in Android Studio?

Once we have our flavors set up, we need to prepare our IDE.

  1. Find “Edit configurations”.
  2. Add a new configuration and set its name.
  3. In the field Build flavor, put the correct flavor name.
The image depicts a screenshot of the IntelliJ IDEA or Android Studio IDE interface, showcasing a Dart project named "flavor_demo".
A screenshot of the "Run/Debug Configurations" window in a development IDE, specifically for Flutter. The configuration is named "Flavor Demo Prod". Fields displayed include "Dart entrypoint", "Additional run args", "Additional attach args", and "Build flavor" which is set to "prod". There are options at the bottom to edit configuration templates and buttons for "Cancel", "Apply", and "OK".

How do you run Flutter flavor in VSCode?

In the project root, create a .vscode directory and generate a launch.json file in it.

A screenshot of a code editor displaying the contents of "launch.json" file. The file contains configuration settings for launching a Dart application. It specifies a version of "0.2.0" and a single configuration named "Flavor Demo Prod" with attributes for launching a Dart program located at "lib/main_development.dart" with arguments for flavor as "prod".

Now, we can launch our application with the newly created Flavor.

A user interface toolbar showing three dropdown selections: "iPhone 14 Pro Max (mobile)", "Flavor Demo Prod", and "Pixel 6 Pro API 34", set against a dark background.

or build it from the command line:

flutter build appbundle –flavor prod

Contact

Do you want to find out more about Flutter app development?

Let's discuss it!

Mastering Flutter application variants with flavors

The power of Flutter flavors truly lies in its ability to streamline and diversify mobile app development. With flavors, we can effortlessly serve our application in various ‘tastes’ while maintaining a single core codebase. This not only simplifies the development process but also ensures efficient bug tracking, seamless environment switching, and personalized application branding. So, the next time you’re in the development ‘kitchen,’ remember to spice things up with some flavors and watch your app evolve in ways you never imagined!