ufk
ufk

Reputation: 32094

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 ? thanks!

Upvotes: 2

Views: 617

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657048

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.

Upvotes: 3

Related Questions