Flutter Flavor:-
I was searching for a flavor option in Flutter, similar to Android, but it was not easy to find a simple and easy solution for me. I did a couple of research on flavor and found this solution that I'm going to share with you.
To achieve flavor in flutter, we have to only do some modification in Android files that's it.
So let's start with Android.
open the build.gradle file and add the following line.
You will get a different package for the different build to upload on play store.
Now let see the Flutter configuration.
In flutter, I have created flavor.dart file to choose the flavor at runtime.
flavor.dart
Let see the implementation of flavor.dart.
Open the main.dart file add the following code.
main.dart
As you can see the above code, I have set the build flavor to free and displayed the conditional widget or to take the conditional action.
After merging the above code, run the command flutter run --flavor free and hit enter.
You change the flavor option as per the requirement.
You will get 2 different app with a different package name.
Thank you for reading.
I was searching for a flavor option in Flutter, similar to Android, but it was not easy to find a simple and easy solution for me. I did a couple of research on flavor and found this solution that I'm going to share with you.
To achieve flavor in flutter, we have to only do some modification in Android files that's it.
So let's start with Android.
open the build.gradle file and add the following line.
android { flavorDimensions "version" productFlavors{ free{ applicationId "com.example.blog.free" } paid{ applicationId "com.example.blog.paid" } } }
You will get a different package for the different build to upload on play store.
Now let see the Flutter configuration.
In flutter, I have created flavor.dart file to choose the flavor at runtime.
flavor.dart
import 'package:meta/meta.dart'; enum BuildFlavor { free, paid } BuildEnvironment _env; BuildEnvironment get env => _env; class BuildEnvironment { final BuildFlavor flavor; BuildEnvironment._init({this.flavor}); static void init({ flavor}) => _env ??= BuildEnvironment._init(flavor: flavor); }As you can see the above code I have created 2 flavors free and paid. Make sure you have an equivalent flavor in an android build.gradle file to create different products or flavor.
Let see the implementation of flavor.dart.
Open the main.dart file add the following code.
main.dart
import 'package:blog/flavor.dart'; import 'package:flutter/material.dart'; void main() { BuildEnvironment.init(flavor: BuildFlavor.free); assert(env != null); runApp( MaterialApp( theme: ThemeData( primarySwatch: Colors.amber, ), debugShowCheckedModeBanner: false, home: Home(), ), ); } class Home extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.grey[300], appBar: AppBar( title: Text('Flutter flavor'), ), body: Center( child: Text( env.flavor == BuildFlavor.free ? 'FREE' : 'PAID', style: TextStyle( fontSize: 28, ), ), ), ); } }
After merging the above code, run the command flutter run --flavor free and hit enter.
You change the flavor option as per the requirement.
You will get 2 different app with a different package name.
Thank you for reading.

