ufk ufk - 2 months ago 8x
Dart Question

defining providers for an angular2 component using dart

I'm writing an angular2 dart application using Intellij.

I created a provider called

that should be injected to the app component.

I defined the Auth service using the following code:

import 'package:angular2/core.dart';
import 'package:auth0_lock/auth0_lock.dart';
import './config.dart';

class Auth {
Auth0Lock lock;

Auth() {
this.lock = new Auth0Lock(configObj.auth0.apiKey, configObj.auth0.domain);
updateProfileName(data) {
var profile = data['profile'] != null ? data['profile'] : data;

login() {
this.lock.show(popupMode: true, options: {'authParams': {'scope': 'openid profile'}}).then(this.updateProfileName);

and the app component using the following code:

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
import 'welcome_component.dart';
import 'auth_service.dart';

selector: 'my-app',
templateUrl: '/www/my-app.html',
providers: [Auth]
@RouteConfig(const [
const Route(path: '/welcome', component: WelcomeComponent, name: 'welcome')
class AppComponent {
Auth auth;
AppComponent(Auth auth) {

now intellij is complaning about the providers array with the error message
arguments of constant creation must be constant expressions

I'm new to dart... but if the Component configuration needs consts, how can I provide classes to be used there ?



Just adding const should do:

providers: const [Auth]

The error you're seeing is because [Auth] creates a List that — although it contains only a const memeber — is itself not constant. (For example, it could be added to, or cleared.) Dart requires you to specify explicitly that the List is constant.