Dolphin
Dolphin

Reputation: 38773

Provide different build configurations / flavors (development, production)

In my flutter app, I have a configuration like this:

api_url = https://api.poemhub.top

the problem is, in different environment the api url is different, when in develop, the api should look like this :

api_url = https://dev-api.poemhub.top

in test env:

api_url = https://beta-api.poemhub.top

in production environment:

api_url = https://api.poemhub.top

also other config different in different places, so what is the best way to treat this config in different environment? I have read a solution to define different main.dart and run build command like this:

flutter build apk -t lib/main_develop.dart

Upvotes: 6

Views: 2790

Answers (3)

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8713

As alternative to flutter flavors suggested by @Pavlo Ostasha you can use flutter flavorizr

  1. In your pubspecs.yaml add

    dependencies: flutter_flavorizr: ^1.0.11
    
  2. Install packages with

    flutter pub get
    
  3. In your dart code use:

    import 'package:flutter_flavorizr/flutter_flavorizr.dart';
    
  1. In your pubspecs.yaml define your flavors, for example test and production like below:
flavorizr:
  app:
    android:
      flavorDimensions: "environment"
    ios:
    
flavors:
    test:
      app:
        name: "Your app - test"

      android:
        applicationId: "com.yourcompany.appid.dev"

      ios:
        bundleId: "com.yourcompany.appid.dev"

    production:
      app:
        name: "Your app"

      android:
        applicationId: "com.yourcompany.appid"
      ios:
        bundleId: "com.yourcompany.appid"
  1. In the generated lib/flavors.dart file, define your getter for the api url
enum Flavor {
  TEST,
  PRODUCTION,
}

class F {
  static Flavor appFlavor;

  static String get apiUrl{
    switch (appFlavor) {
      case Flavor.TEST:
        return 'http://www.api-test.com';
      case Flavor.PRODUCTION:
        return 'http://www.api.com';
      default:
        return 'http://www.api.com';
    }
  }
}
  1. In your code do
import 'package:app/flavors.dart';

class YourApp extends StatelessWidget {
  const YourApp({Key key}) : super(key: key);

  var apiUrl = F.apiUrl; // from flavors.dart
}

Upvotes: 1

Pavlo Ostasha
Pavlo Ostasha

Reputation: 16699

I would suggest to go with flutter flavors. This feature does exactly what you want.

Upvotes: 5

Nagual
Nagual

Reputation: 2088

as an option you can put this code somewhere

const APP_ENV = String.fromEnvironment('APP_ENV');

and build project with parameter DEV or PROD for example
flutter build apk -t lib/main_develop.dart --dart-define=APP_ENV=DEV

extended example

enum AppEnvironment { DEV, PROD, MOCK }

class EnvironmentConfig {
  static const APP_ENV = String.fromEnvironment('APP_ENV');
  static const LOG = String.fromEnvironment('LOG');

  AppEnvironment get appEnvironment => APP_ENV.isEmpty ? AppEnvironment.MOCK
      : AppEnvironment.values.firstWhere((e) => e.toString() == 'AppEnvironment.' + APP_ENV);

  String get baseUrl => appEnvironment == AppEnvironment.DEV ? 'https://some dev url'
      : appEnvironment == AppEnvironment.PROD ? 'https://some url'
      : 'https://mock';

}

and then

if (EnvironmentConfig().appEnvironment == AppEnvironment.DEV) {
   // code
}

Upvotes: 3

Related Questions