2016-08-04 123 views
1

我在app.config.dart创建了一个const对象用下面的代码:如何从const对象中获取值?

const configObj = const { 
'webServer': const { 
    'appBaseHref' : "/" 
}, 
'auth0': const { 
    'apiKey': "foo", 
    'domain': "bar", 
    'callbackUrl': "callback" 
} 
}; 

现在我的主要镖文件I导入app.config.dart,我尝试到那里的价值观和理念,现在该怎么做。 configObj.auth0.apiKey产生错误EXCEPTION: Class 'ImmutableMap' has no instance getter 'auth0'

那么我该怎么做?

谢谢!

回答

3

镖不支持访问与.

映射条目它应该是:

configObj['auth0']['apiKey']; 

另外,您可以为您的配置创建类,如

class WebServerConfig { 
    final String appBaseHref; 
    const WebServerConfig(this.appBaseHref); 
} 

class Auth0Config { 
    final String apiKey; 
    final String domain; 
    final String callbackUrl; 
    const Auth0(this.apiKey, this.domain, this.callbackUrl); 
} 

class MyConfig { 
    final WebServerConfig webServer; 
    final Auth0Config auth0; 
    const MyConfig(this.webServer, this.auth0);  
} 

const configObj = const MyConfig(
    const WebServerConfig("/"), 
    const Auth0Config(
    "foo", 
    "bar", 
    "callback" 
) 
); 

这种方式,你也当您访问配置属性时可以正确自动完成,并且可以使用简单的.表示法来访问属性。