ufk ufk - 1 year ago 93
Dart Question

defining static const objects in dart

I'm learning dart. and I'm having trouble creating a simple static const object that contains some configuration parameters.

this is how I define the object in javascript:

var configObj = {
webServer: {
appBaseHref : "/"
auth0: {
apiKey: "<API_KEY>",
domain: "<DOMAIN>",
callbackUrl: "<CALLBACK_URL>"

how do I convert that to dart ?
do I need to create a class and init it with the relevant params or is there a simpler way to define static objects ?

Answer Source
const configObj = const {
 'webServer': const {
     'appBaseHref' : "/"
 'auth0': const {
     'apiKey': "<API_KEY>",
     'domain': "<DOMAIN>",
     'callbackUrl': "<CALLBACK_URL>"

Dart allows other types as keys in maps than strings, this is why string keys need the quotes. To make values const in Dart use the const keyword. Sub-objects need to be made const individually.

There are discussions to derive from the scope whether const is required and apply it automatically probably in Dart 2.0.