Comparing plain old Dart, JSON, YAML, and environment variables
By now you’re used to setting configuration options in pubspec.yaml, but what if you want to define your own config values and read them inside your app? How do you do that? I’ll give you an overview of some options in this article.
Plain old Dart
One easy solution is to just use a plain old Dart class. Create a file named my_config.dart and add the following code:
class MyConfig {
static const String country = 'Mongolia';
static const String animal = 'horse';
}
Then you can use it anywhere in your code by importing that file:
import 'my_config.dart';void main() {
print(MyConfig.country);
print(MyConfig.animal);
}
Using a library rather than a class
There is really no need to use a class name like MyConfig
when all it contains is static members. Replace my_config.dart with the following simplified code:
const String country = 'Mongolia';
const String animal = 'horse';
You can still use these values anywhere in your app, but now you can also give them any namespace and show or hide them…