引用站外地址
参考地址1: Flutter如何快速切换生产和测试环境
https://www.zhihu.com/tardis/bd/art/427328432?source_id=1001
引用站外地址
参考地址2: flutter多环境配置及Dio请求封装
https://www.codenong.com/cs107093912/
内容整合enum-> env
环境枚举配置
lib/enum/env.dart
1
| enum Env { production, development, local }
|
config-> env_config
环境配置文件env_config!
lib/config/env_config.dart
env_config.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import 'package:flutter/material.dart'; import 'package:rent_house/enum/env.dart';
class EnvConfig extends InheritedWidget { static Enum envName = Env.development; static String appName = "好客租房"; static String baseUrl = getBaseUrlByEnum(Env.development)!;
EnvConfig({ super.key, required String appName, required Enum envName, required String baseUrl, required Widget child, }) : super(child: child) { EnvConfig.appName = appName; EnvConfig.envName = envName; EnvConfig.baseUrl = baseUrl; } static EnvConfig? of(BuildContext context) { return context.dependOnInheritedWidgetOfExactType(aspect: EnvConfig); }
static bool? isProduction() => const bool.fromEnvironment("dart.vm.product"); static List<Map<Enum, String>> baseUrlList = [ { Env.production: 'https://www.fastmock.site/mock/04856e89f6ef8eca01f7bab4c6166bc6/rent_house/api/' }, { Env.development: 'https://www.fastmock.site/mock/240180fe470de74ae9e92a6e9a9f9d1e/rent_house_test/api/' }, { Env.local: 'https://www.fastmock.site/mock/240180fe470de74ae9e92a6e9a9f9d1e/rent_house_test/api/' }, ];
static String? getBaseUrlByEnum(Enum env) {
return baseUrlList.singleWhere((element) => element[env] != null)[env]; }
static String? getBaseUrl() { var envEnum = isProduction()! ? Env.production : Env.development; return getBaseUrlByEnum(envEnum); }
@override bool updateShouldNotify(covariant InheritedWidget oldWidget) { return false; } }
|
新建main环境入口文件
主要包含local.dart dev.dart prod.dart 分别为本地环境 开发环境 生产环境!
lib/main/*.dart
local.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import 'package:fconsole/fconsole.dart'; import 'package:flutter/material.dart'; import 'package:rent_house/app.dart'; import 'package:rent_house/config/env_config.dart'; import 'package:rent_house/enum/env.dart';
void main() { WidgetsFlutterBinding.ensureInitialized(); var configuredApp = EnvConfig( appName: '好客煮饭local', envName: Env.local, baseUrl: EnvConfig.getBaseUrlByEnum(Env.local)!, child: const App(), ); runAppWithFConsole(configuredApp); }
|
dev.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import 'package:fconsole/fconsole.dart'; import 'package:flutter/material.dart'; import 'package:rent_house/app.dart'; import 'package:rent_house/config/env_config.dart'; import 'package:rent_house/enum/env.dart';
void main() { WidgetsFlutterBinding.ensureInitialized(); var configuredApp = EnvConfig( appName: '好客煮饭dev', envName: Env.development, baseUrl: EnvConfig.getBaseUrlByEnum(Env.development)!, child: const App(), ); runAppWithFConsole(configuredApp); }
|
prod.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import 'package:flutter/material.dart'; import 'package:rent_house/app.dart'; import 'package:rent_house/config/env_config.dart'; import 'package:rent_house/enum/env.dart';
void main() { WidgetsFlutterBinding.ensureInitialized(); var configuredApp = EnvConfig( appName: '好客煮饭prod', envName: Env.production, baseUrl: EnvConfig.getBaseUrlByEnum(Env.production)!, child: const App(), ); runApp(configuredApp); }
|
配置启动文件vscode
我这边idea用的vscode, 接下来需要配置下启动配置文件launch.json!
mac 用户: command + shift + p 打开 搜索窗口 输入 launch.json 打开文件!
windows 用户: ctrl + shift + p 打开 搜索窗口 输入 launch.json 打开文件!
配置内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| "configurations": [ { "name": "rent_house_local", "type": "dart", "request": "launch", "program": "lib/main/local.dart" }, { "name": "rent_house_dev", "type": "dart", "request": "launch", "program": "lib/main/dev.dart" }, { "name": "rent_house_prod", "type": "dart", "request": "launch", "program": "lib/main/prod.dart" }, ]
|
结果如下:

lib/confg/http_config.dart
1 2 3 4 5 6 7 8 9
| import 'env_config.dart';
class HTTPConfig { static String? baseURL = EnvConfig.baseUrl; static const timeout = 60 * 1000; }
|
环境运行
- 通过
vscode配置的启动文件!
- 通过
flutter指定入口文件!
启动命令1 2 3
| flutter run -t lib/main/local.dart flutter run -t lib/main/dev.dart flutter run -t lib/main/prod.dart
|
打包命令1 2 3
| flutter build apk -t lib/main/local.dart flutter build apk -t lib/main/dev.dart flutter build apk
|
lib/app.dart
app.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart'; import 'package:rent_house/config/env_config.dart'; import 'package:rent_house/pages/unknow/index.dart'; import 'package:rent_house/router/app_pages.dart'; import 'package:rent_house/utils/storage/index.dart';
class App extends StatelessWidget { const App({super.key});
@override Widget build(BuildContext context) {
EnvConfig.of(context); print( "envConfig appName ${EnvConfig.appName}", ); print("envConfig baseUrl ${EnvConfig.baseUrl}"); print("envConfig envName ${EnvConfig.envName}"); EasyLoading.instance.indicatorType = EasyLoadingIndicatorType.chasingDots; return ScreenUtilInit( designSize: const Size(375, 812), builder: (context, child) => GetMaterialApp( title: EnvConfig.appName, theme: ThemeData( primarySwatch: Colors.teal, ), routingCallback: (routing) { print('routingCallback value $routing ${routing!.current}'); print('routingCallback ${LocalStorage.get("authInfo")}'); }, locale: const Locale('zh'), localizationsDelegates: const [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], supportedLocales: const [ Locale('zh'), Locale('en'), ],
unknownRoute: GetPage( name: Routes.UNKNOW, page: () => const UnknowPage(), transition: Transition.downToUp), debugShowCheckedModeBanner: false, initialRoute: AppPages.INITIAL, getPages: AppPages.routes, defaultTransition: Transition.rightToLeft, builder: EasyLoading.init(), ), );
} }
|