DART 中可能有配置文件吗?

is possible have a configuration file in DART?

我有这个JavaScriptclass:

'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'
  }
}

然后像这样使用它

main.dart

import 'my_config.dart';

main() {
  print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}

或者您可以使用 类 获得正确的自动完成和类型检查

my_config.dart

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);
}

main.dart

import 'my_config.dart';

main() {
  print(properties.viewElements.splashClass);
}

根据上面的答案用classes跟进,实现静态变量可能比较方便,缺点还是必须是compiled/rebuilt.

class CONFIG {
  static final String BUILD = "Release";
  static final String DEPLOYMENT = "None";
}

这可以在导入后从单独的 class 使用:

var xyz = CONFIG.BUILD;