在DART中是否可能有配置文件?
我有一个JavaScript类:在DART中是否可能有配置文件?,dart,properties-file,Dart,Properties File,我有一个JavaScript类: 'use strict;' /* global conf */ var properties = { 'PROPERTIES': { 'CHANNEL': 'sport', 'VIEW_ELEMENTS': { 'LOADER_CLASS': '.loader', 'SPLASH_CLASS': '.splash' } } }; 在JavaScri
'use strict;'
/* global conf */
var properties = {
'PROPERTIES': {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
};
在JavaScript中,我可以使用以下属性:properties.properties.CHANNEL
是否可以将此转换为DART?有没有最好的方法可以做到这一点?有不同的方法 你可以创建一张地图 my_config.dart
const Map properties = const {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': const {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
import 'my_config.dart';
main() {
print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}
const properties = const Properties('sport', const ViewElements('.loader', '.splash'));
class Properties {
final String channel;
final ViewElements viewElements;
const Properties(this.channel, this.viewElements;
}
class ViewElements {
final String loaderClass;
final String splashClass;
const ViewElements(this.loaderClass, this.splashClass);
}
import 'my_config.dart';
main() {
print(properties.viewElements.splashClass);
}
然后像这样使用它
main.dart
const Map properties = const {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': const {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
import 'my_config.dart';
main() {
print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}
const properties = const Properties('sport', const ViewElements('.loader', '.splash'));
class Properties {
final String channel;
final ViewElements viewElements;
const Properties(this.channel, this.viewElements;
}
class ViewElements {
final String loaderClass;
final String splashClass;
const ViewElements(this.loaderClass, this.splashClass);
}
import 'my_config.dart';
main() {
print(properties.viewElements.splashClass);
}
或者您可以使用类来获得正确的自动完成和类型检查
my_config.dart
const Map properties = const {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': const {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
import 'my_config.dart';
main() {
print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}
const properties = const Properties('sport', const ViewElements('.loader', '.splash'));
class Properties {
final String channel;
final ViewElements viewElements;
const Properties(this.channel, this.viewElements;
}
class ViewElements {
final String loaderClass;
final String splashClass;
const ViewElements(this.loaderClass, this.splashClass);
}
import 'my_config.dart';
main() {
print(properties.viewElements.splashClass);
}
main.dart
const Map properties = const {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': const {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
import 'my_config.dart';
main() {
print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}
const properties = const Properties('sport', const ViewElements('.loader', '.splash'));
class Properties {
final String channel;
final ViewElements viewElements;
const Properties(this.channel, this.viewElements;
}
class ViewElements {
final String loaderClass;
final String splashClass;
const ViewElements(this.loaderClass, this.splashClass);
}
import 'my_config.dart';
main() {
print(properties.viewElements.splashClass);
}
在使用类实现上述答案之后,实现静态变量可能很方便,但缺点是仍然必须编译/重建静态变量
class CONFIG {
static final String BUILD = "Release";
static final String DEPLOYMENT = "None";
}
通过以下方式导入后,可以从单独的类中使用:
var xyz = CONFIG.BUILD;
我也会选择第二种,但来自JS的人通常喜欢较少的课程。但是,如果对浏览器应用程序使用这些解决方案之一,则需要使用应用程序编译配置。