运行命令:
flutter run --dart-define=DART_DEFINE_APP_ENV=debug // 开发
flutter run --dart-define=DART_DEFINE_APP_ENV=test // 测试
flutter run --dart-define=DART_DEFINE_APP_ENV=pre // 灰度
flutter run --dart-define=DART_DEFINE_APP_ENV=release // 生产
打包命令:
flutter build --dart-define=DART_DEFINE_APP_ENV=debug // 开发
flutter build --dart-define=DART_DEFINE_APP_ENV=test // 测试
flutter build --dart-define=DART_DEFINE_APP_ENV=pre // 灰度
flutter build --dart-define=DART_DEFINE_APP_ENV=release // 生产
Android:flutter build apk ...
IOS:flutter build ios ...
utils:env_config.dart
// 环境配置
class EnvConfig {
final String appTitle;
final String appDomain;
EnvConfig({
required this.appTitle,
required this.appDomain,
});
}
// 环境声明
abstract class EnvState {
// 环境key
static const String envKey = "DART_DEFINE_APP_ENV";
static const String debug = "debug";
static const String test = "test";
static const String pre = "pre";
static const String release = "release";
}
class Env {
// 获取当前环境
static const appEnv = String.fromEnvironment(EnvState.envKey);
static final EnvConfig _debugConfig = EnvConfig(
appTitle: "开发",
appDomain: "https://dev-www.sucoty.com",
);
static final EnvConfig _testConfig = EnvConfig(
appTitle: "测试",
appDomain: "https://test-www.sucoty.com",
);
static final EnvConfig _preConfig = EnvConfig(
appTitle: "灰度",
appDomain: "https://pre-www.sucoty.com",
);
static final EnvConfig _releaseConfig = EnvConfig(
appTitle: "生产",
appDomain: "https://www.sucoty.com",
);
static EnvConfig get envConfig => _getEnvConfig();
static EnvConfig _getEnvConfig() {
switch(appEnv) {
case EnvState.debug:
return _debugConfig;
case EnvState.test:
return _testConfig;
case EnvState.pre:
return _preConfig;
case EnvState.release:
return _releaseConfig;
default:
return _debugConfig;
}
}
}
引用:
import 'package:sucoty_app/utils/env_config.dart';
static String BASE_URL = Env.envConfig.appDomain;
... // TODO OTHER THINGS