【问题标题】:is possible have a configuration file in DART?是否可以在 DART 中有配置文件?
【发布时间】:2016-06-21 06:01:01
【问题描述】:

我有这个 JavaScript 类:

'use strict;'
/* global conf */

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

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

是否可以将其转换为 DART?有最佳做法吗?

【问题讨论】:

    标签: dart properties-file


    【解决方案1】:

    有不同的方法。

    你可以创建一个地图

    my_config.dart

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

    然后像这样使用它

    ma​​in.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);
    }
    

    ma​​in.dart

    import 'my_config.dart';
    
    main() {
      print(properties.viewElements.splashClass);
    }
    

    【讨论】:

    • 我也会选择第二名,但来自 JS 的人通常更喜欢少上课。但是,将这些解决方案之一用于浏览器应用程序,您需要使用应用程序编译配置。
    【解决方案2】:

    使用类跟进上述答案,实现静态变量可能很方便,缺点是它仍然必须编译/重建。

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

    这可以在通过以下方式导入后从单独的类中使用:

    var xyz = CONFIG.BUILD;
    

    【讨论】:

      猜你喜欢
      • 2021-08-26
      • 1970-01-01
      • 2017-06-08
      • 2018-10-19
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2015-09-17
      相关资源
      最近更新 更多