DART 中可能有配置文件吗?

And*_*zza 7 properties-file dart

我有这个 JavaScript 类:

'use strict;'
/* global conf */

var properties = {
    'PROPERTIES': {
        'CHANNEL': 'sport',
        'VIEW_ELEMENTS': {
            'LOADER_CLASS': '.loader',
            'SPLASH_CLASS': '.splash'
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

在 JavaScript 中,我可以使用这些属性: properties.PROPERTIES.CHANNEL

是否可以将其转换为 DART?有没有最好的做法来做到这一点?

Gün*_*uer 6

有不同的方式。

你可以只创建一张地图

my_config.dart

const Map properties = const {
  'CHANNEL': 'sport',
  'VIEW_ELEMENTS': const {
    'LOADER_CLASS': '.loader',
    'SPLASH_CLASS': '.splash'
  }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它

main.dart

import 'my_config.dart';

main() {
  print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

main.dart

import 'my_config.dart';

main() {
  print(properties.viewElements.splashClass);
}
Run Code Online (Sandbox Code Playgroud)