foreword
Because the front end writes too much, I always think about using cross-env to mix in system variables. However, the debugger of vscode is a bit unworkable, so I found a new way
Singleton mode AppConfig
AppConfig.dart
import 'package:flutter/material.dart';
enum DevMode { sf, ad }
class AppConfig {
DevMode mode;
static AppConfig _appConfig;
factory AppConfig(DevMode mode) {
if (_appConfig != null)
return _appConfig;
else {
_appConfig = AppConfig._init(mode);
return _appConfig;
}
}
static AppConfig getConfig() {
if (_appConfig == null) return throw new ErrorSummary("需要初始化");
return _appConfig;
}
AppConfig._init(this.mode);
}
Then by default you already have an main.dart
exposed MyApp
Widget component, and then create two entry files for different environments, just show one below
sf. dart
import './config.dart';
import 'package:flutter/material.dart';
import './main.dart';
void main() {
AppConfig(DevMode.sf);
runApp(MyApp());
}
Finally, configure vscode launch.json
and it will be ok
// ...
"configurations": [
{
"name": "sf mode",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"program": "${workspaceFolder}\\lib\\sf.dart"
},
{
"name": "ad mode",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"program": "${workspaceFolder}\\lib\\ad.dart"
}
]
var config = AppConfig.getConfig();
Just use it