Logo

dev-resources.site

for different kinds of informations.

Create Different Type of Flavor on Flutter Application

Published at
1/9/2025
Categories
flutter
dart
mobile
webdev
Author
ssoad
Categories
4 categories in total
flutter
open
dart
open
mobile
open
webdev
open
Author
5 person written this
ssoad
open
Create Different Type of Flavor on Flutter Application

Flutter is a versatile framework that empowers developers to build cross-platform applications with ease. One of its powerful features is the ability to create multiple flavors of your app. This is particularly useful when you want to manage different environments (e.g., development, staging, production) or create white-labeled versions of your application for different clients.

In this post, weโ€™ll explore how to set up and manage flavors in a Flutter 3.27.1 project step by step, leveraging the latest features and updates in this version.

What Are Flavors in Flutter?

In the context of Flutter, flavors are essentially different configurations of your app that may vary in API endpoints, app names, icons, or any other environment-specific settings. These configurations allow you to:

Use different app names, package IDs, and icons.

Target different API environments (development, staging, production).

Customize app behavior for specific use cases.

Flutter 3.27.1 enhances flavor support with better integration in the build system, making the setup smoother than ever.

Setting Up Flavors in Flutter 3.27.1

Letโ€™s create multiple flavors for a Flutter app. For this guide, weโ€™ll set up development, staging, and production flavors.

Step 1: Configure Android Flavors

  1. Update android/app/build.gradle: Add the flavor configurations under the android block. Flutter 3.27.1 ensures better support for Gradle versions.
android {
    ...
    flavorDimensions "default"
    productFlavors {
        dev {
            dimension "default"
            applicationIdSuffix ".dev"
            versionNameSuffix "-dev"
        }
        staging {
            dimension "default"
            applicationIdSuffix ".staging"
            versionNameSuffix "-staging"
        }
        prod {
            dimension "default"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create Separate Resources for Each Flavor:
    Place different icons and configurations in directories like res/mipmap-dev, res/mipmap-staging, and res/mipmap-prod. Flutter 3.27.1โ€™s build system ensures seamless resource handling.

  2. Update android/app/src/main/AndroidManifest.xml:
    Use placeholders for app-specific values:

<application
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher">
</application>
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure iOS Schemes

  1. Create New Schemes:
  2. Open the iOS project in Xcode.
  3. Go to Product > Scheme > Manage Schemes and duplicate the existing scheme for dev, staging, and prod.

  4. Add Configurations in ios/Runner.xcodeproj:

  5. Select the project in Xcode and go to the Info tab.

  6. Add configurations for Debug-dev, Release-staging, and so on.

  7. Set Build Settings for Each Flavor:

  8. Update Bundle Identifier (e.g., com.example.app.dev, com.example.app.staging, com.example.app).

  9. Use different xcassets for app icons and splash screens. Flutter 3.27.1 improves asset handling for iOS.

Step 3: Use Environment-Specific Configurations in Dart

  1. Create a Dart Configuration File: Create a flavors.dart file to define environment-specific variables:
class FlavorConfig {
  final String name;
  final String baseUrl;

  static FlavorConfig? _instance;

  FlavorConfig._internal({required this.name, required this.baseUrl});

  static void initialize({required String name, required String baseUrl}) {
    _instance = FlavorConfig._internal(name: name, baseUrl: baseUrl);
  }

  static FlavorConfig get instance => _instance!;
}
Enter fullscreen mode Exit fullscreen mode
  1. Initialize the Flavor: Update the main method to initialize the flavor:
void main() {
  FlavorConfig.initialize(
    name: "Development",
    baseUrl: "https://dev.api.example.com",
  );
  runApp(MyApp());
}
Enter fullscreen mode Exit fullscreen mode
  1. Pass the Flavor During Build: Use the flutter run command with --flavor:
flutter run --flavor dev -t lib/main_dev.dart
flutter run --flavor staging -t lib/main_staging.dart
flutter run --flavor prod -t lib/main_prod.dart
Enter fullscreen mode Exit fullscreen mode

Benefits of Using Flavors in Flutter 3.27.1

  • Streamlined Development and Testing: Quickly switch between environments without modifying the codebase. Flutter 3.27.1 makes environment transitions smoother.

  • Customization: Easily differentiate app branding for different clients or use cases.

  • Scalability: Manage multiple app versions in a single codebase efficiently.

  • Improved Performance: The enhanced build process in Flutter 3.27.1 optimizes flavor-specific builds, reducing build times.

Best Practices for Flavor Management

  • Use Environment Files: Maintain separate environment files for each flavor to manage configurations effectively.

  • Automate Builds: Use CI/CD tools like GitHub Actions or Bitrise to automate flavor builds.

  • Document Flavors: Clearly document the purpose and configurations of each flavor for your team.

Conclusion

Setting up flavors in a Flutter app with version 3.27.1 is more efficient and powerful than ever. By leveraging its enhanced build tools and configurations, you can create and manage multiple app versions tailored to your needs effortlessly.

If you found this guide helpful, feel free to connect with me for more insights and discussions:

GitHub: Sohanuzzaman Soad

LinkedIn: Sohanuzzaman Soad

Happy coding! ๐Ÿš€

flutter Article's
30 articles in total
Favicon
Flutter vs React Native in 2025: A Comprehensive Comparison
Favicon
Building a Cross-Platform Food Ordering App with Flutter
Favicon
Deploying Flutter Web Apps using Globe.dev
Favicon
Flutter App, Speech to Text and Text to Speech ๐Ÿฃ
Favicon
Journey to Clean Architecture: Wrestling with a 10k Line Flutter Legacy Codebase
Favicon
Building the 'One of a Kind' Ultimate Mobile App Framework. Seeking exceptional engineers to join the journey.
Favicon
Why Should You Develop a Native Android App Over Flutter?
Favicon
Flutter Development for Low end PCs
Favicon
5 Essential Flutter Widgets Every Developer Should Master
Favicon
Mastering Nested Navigation in Flutter with `go_router` and a Bottom Nav Bar
Favicon
Flutter for Beginners: From Installation to Your First App
Favicon
Building a Beautiful Login Screen in Flutter: A Complete Guide
Favicon
Flutter Design Pattern Bussines Logic Component (BLOC)
Favicon
Movie X: A Developerโ€™s Dive Into Flutter Project Organization
Favicon
Create Different Type of Flavor on Flutter Application
Favicon
Handling PathAccessException in iOS for File Download
Favicon
Full Stack Development (Mern && Flutter)
Favicon
Common mistakes in Flutter article series
Favicon
Design Pattern in Flutter MVVM
Favicon
7 Ways to Refactor Your Flutter Application
Favicon
is there any good article for custom lint rules in flutter?
Favicon
WebRTC vs Agora Video SDK vs ZegoCloud for Video Calling in Flutter: A Comprehensive Comparison
Favicon
Hassle free flutter Development in Hyprland with Neovim
Favicon
How to Build a CRUD Application Using Flutter & Strapi
Favicon
Flutter vs. React Native: Which mobile framework will you choose in 2025?
Favicon
Understanding ShellRoute in go_router: Managing Shared Layouts Effectively
Favicon
From Chaos to Control: The Day I Learned the Magic of Debouncing in Flutter ๐Ÿš€
Favicon
custom lint rules in flutter
Favicon
How to Clear Cookies in Flutter Custom Tabs?
Favicon
Flutter

Featured ones: