Can you mix regular and injected parameters in a constructor in Angular Dart?
-
20-12-2019 - |
Domanda
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');
Soluzione
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow