7

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?

Hexaholic
  • 3,299
  • 7
  • 30
  • 39
Andrea Bozza
  • 1,374
  • 2
  • 12
  • 31

2 Answers2

7

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);
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    I would also go for the 2nd but people coming from JS often prefer less classes. Using one of these solutions for browser applications you need to compile the configuration with the application though. – Günter Zöchbauer Mar 07 '16 at 16:07
2

Following up on the above answer using classes, it may be convenient to implement static variables, the downside is that it still must be compiled/rebuilt.

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

This can be used from a separate class after importing via:

var xyz = CONFIG.BUILD;
gieoon
  • 129
  • 7