Config product flavor
  • 🥳Config Build Flavor on IOS
  • 😍Config Build Flavor on Flutter
Powered by GitBook
On this page

Config Build Flavor on Flutter

After adding environments for Ios and Android platform we need to config on Flutter project with some information like API_URL for staging, nightly, and production.

  1. Create EnvironmentDefault class like this:

abstract class EnvironmentDefault {
  abstract var API_URL;
  static EnvironmentDefault getInstance() {
    if (_instance == null) {
      throw ("EnvironmentDefault _instance is null ");
    }
    return _instance!;
  }

  EnvironmentDefault() {
    _instance = this;
  }
}
  1. Create Environment Nightly, Staging, Production extends EnvironmentDefault

Nightly:

class EnvironmentNightly extends EnvironmentDefault {
  EnvironmentNightly() : super();
  @override
  var API_URL = "https://nightly.company.com";
}

Staging:

class EnvironmentStaging extends EnvironmentDefault {
  EnvironmentStaging() : super();
  @override
  var API_URL = "https://stg.company.com";
}

Production:


class EnvironmentProduction extends EnvironmentDefault {
  EnvironmentProduction() : super();
  @override
  var API_URL = "https://production.company.com";
}
  1. Create 3 main classes called:

  • main_stg.dart with environment staging

main_stg.dart
void main() async {
    EnvironmentStaging();
    runApp(App());
  }
  • main_nightly.dart with environment nightly

main_nightly.dart
void main() async {
  EnvironmentNightly();
  runApp(App());
}
  • main_production.dart with environment production

main_production.dart
void main() async {
  EnvironmentProduction();
  runApp(App());
}

In your code, if you need to get API_URL for the right environment just use:

EnvironmentDefault.getInstance().API_URL

Build the project with your flavor, and open "Edit configurations" in Android Studio:

Done!!

PreviousConfig Build Flavor on IOS

Last updated 2 years ago

😍