Question

In Angular Dart, can you mix regular parameters and injected parameters in a class constructor? When I instantiate the class, I get a missing arguments error. For example:

If you have:

class Foo {
  String b;
  Http _http;

  Foo(String this.b, Http this._http);
}

Foo foo = new Foo('beta'); 

//Error missing arguments.

My work around

class Foo {
  String b;
  Http _http;

  Foo(Http this._http);
}

Foo foo = new Foo(); 
foo.b = 'beta';

What I would like:

class Foo {
  String b;
  Http _http;

  Foo(String this.b) {
    //inject an instance of _http here?
  }
}

Foo foo = new Foo('beta'); 
Was it helpful?

Solution

You can pass the injector as a second parameter and let your Foo class' constructor pull the HTTP from the injector
like show in the answer to this question Manual injection in Dart

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top