is possible have a configuration file in DART?

断了今生、忘了曾经 提交于 2019-12-12 12:27:16

问题


I have this JavaScript class:

'use strict;'
/* global conf */

var properties = {
    'PROPERTIES': {
        'CHANNEL': 'sport',
        'VIEW_ELEMENTS': {
            'LOADER_CLASS': '.loader',
            'SPLASH_CLASS': '.splash'
        }
    }
};

In JavaScript I can use these properties: properties.PROPERTIES.CHANNEL

Is it possible to convert this to DART? Is there a best practise to do that?


回答1:


There are different way.

You could just create a map

my_config.dart

const Map properties = const {
  'CHANNEL': 'sport',
  'VIEW_ELEMENTS': const {
    'LOADER_CLASS': '.loader',
    'SPLASH_CLASS': '.splash'
  }
}

then use it like

main.dart

import 'my_config.dart';

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

or you can use classes to get proper autocompletion and type checking

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


来源:https://stackoverflow.com/questions/35847932/is-possible-have-a-configuration-file-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!