11

Is it possible to get flavor name Flutter side? for both android and ios

build.gradle

flavorDimensions "app"

productFlavors {
    dev {
        dimension "app"
        versionCode 2
        versionName "1.0.0"
    }
    qa {
        dimension "app"
        applicationId "com.demo.qa"
        versionCode 3
        versionName "1.0.2"
    }
}
BIS Tech
  • 12,833
  • 7
  • 71
  • 107
  • 1
    What is a main purpose of getting flavor name? Use different configuration based on flavor name in the dart code? Or you just want to get flavor name? – tatsuDn May 12 '20 at 08:18

3 Answers3

17

As long as every flavor has a different packageName you could do it like this:

enum EnvironmentType { dev, qa }

class Environment {
  EnvironmentType current;

  Environment() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      switch (packageInfo.packageName) {
        case "com.demo.qa":
          current = EnvironmentType.qa;
          break;
        default:
          current = EnvironmentType.dev;
      }
    });
  }
}
Josh
  • 681
  • 3
  • 11
  • Nice solution, but packag_info was replaced by a community version that depends on fullter framework and can't run as the first line in main – Asaf Pinhassi Jan 14 '22 at 18:25
  • This shouldn't be a problem as you could just instantiate the class after Flutter is loaded. One possibility would be to add a TransitionBuilder to the MaterialApp and setup your dependencies there. – Josh Jan 17 '22 at 10:06
  • I needed it to init Sentry logging - it needs to be way before the UI – Asaf Pinhassi Jan 19 '22 at 21:44
  • Note this doesn't work on web. Use Till Friebe's solution instead. – E. Sun Mar 18 '22 at 15:57
14

You could use the solution from jonahwilliams.

  1. Set a key-value pair when creating the app:
flutter build apk --flavor=paid --dart-define=app.flavor=paid
  1. Access the value in Dart:
const String flavor = String.fromEnvironment('app.flavor');

void main() {
  print(flavor);
}

This would print "paid" when run.

The advantages are that you can use this on platforms where flavors are not supported yet and that the variable is const. The disadvantage that you have to set the key-value pair for the build.

Till Friebe
  • 886
  • 9
  • 24
  • This is exactly what I used. Works great when building for android because you can pass it with the shell command. Still need to know how to pass dart-define when building an archive from xcode. – shadysamir Dec 30 '20 at 20:30
  • @shadysamir it's similar to above when building from the command line. `flutter build ipa --flavor=paid --dart-define=app.flavor=paid` This link will help a bit. https://docs.flutter.dev/deployment/ios – Nick N Jan 17 '22 at 19:05
  • @nick yeah shell build archive was not released yet in stable back then. But it's been for a while now. – shadysamir Jan 18 '22 at 20:04
3

There is not a simple way to know the flavor name, however I would suggest you to use an environment variable, loaded from flutter_dotenv for example.

file .env

FLAVOR=dev

file main.dart

void main() async {
  await DotEnv().load('.env');
  final flavor = DotEnv().env['FLAVOR'];

  String baseUrl;
  if (flavor == 'dev') {
    baseUrl = 'https://dev.domain.com';
  } else if (flavor == 'qa') {
    baseUrl = 'https://qa.domain.com';
  } else {
    throw UnimplementedError('Invalid FLAVOR detected');
  }
}

This will allow you (as developer) to easily change the behaviour of your app and switch from different environments seamlessy.

NeoKree
  • 402
  • 4
  • 13