What is the best way to set a default instance for a trailing private field with an optional parameter?
For a class with a trailing private field _bar:
class Foo() {
final Bar _bar;
Foo({Bar bar}) : _bar = bar {
//error: list cannot be used as setter, it is final
_bar = new Bar();
}
}
Trying to set it in the parameter list will result in an error
//error: default values of an object must be constant
Foo({Bar bar: new Bar()}) : _bar = bar ..
I would like to keep an optional parameter so that I can inject mocks during unit tests. What's the best approach to do this?
+3
source to share