How do I identify which environment I run the project?

Viewed 914

Is it possible to identify which environment I run the project?

flutter run --flavor prod 
flutter run --flavor dev

dart side

  @override
  void initState() {

    //THIS SHOULD BE WORK BOTH IOS AND ANDORID

    const bool isProduction = bool.fromEnvironment('com.demo.store.prod');
    if(isProduction) {
      print('it is prod');
    } else {
      print('it is not prod');//always print this line
    }
    super.initState();
  }

app/build.gradle - android side

productFlavors {
    dev {
        dimension "app"
        versionCode 1
        versionName "1.0.0"
    }
    prod {
        dimension "app"
        applicationId "com.demo.store.prod"
        versionCode 2
        versionName "1.0.0"
    }
}
1 Answers

Fortunately, flutter doesn't inject any info from Flavors into the Dart code. The maximum that Flutter provides from the box is the ability to identify if it's release build or not with this line

const bool isProduction = bool.fromEnvironment('dart.vm.product');

Also, you can use a package like package_info to get info about the application.

But, if you are partially curious how to configure multiple environments, I would recommend you to use compile-time variables. You can specify them with --dart-define key in flutter run or flutter build command.

After you do this, provided key/value pairs will be available in your Dart and native layers. You can use them to specify application prefix for example. You can read more about how to get them in Gradle and iOS Schemes in this article.

Related