You can use global variables, for example:
DB_URL = 'localhost:5432/mydb'; DB_PASS = 'my_pass';
then you can create a different configuration file for each environment. For example, for production, you can create a production_config.dart file that can contain:
loadConfig() { DB_URL = '123.123.123.123:5432/mydb'; DB_PASS = 'my_prod_pass'; }
Then in your main function you can call production_config.loadConfig if the environment is production, for example:
import 'production_config.dart' as prodConfig; main(List<String> args) { var ENV = getEnvFromArgs(args); if(ENV == 'PROD') { prodConfig.loadConfig(); }
Thus, if you want to move from development to production, you need to pass an argument to your dart program, for example:
dart myprogram.dart -env=PROD
The advantages of this approach are that for this you do not need to create separate properties, a json or yaml file, and you do not need to analyze them. In addition, properties are type-ckecked.
source share