How to specify a default value of a function parameter?
if you want to initialize a Function parameter that is also a field of your class I suggest:
class MyClass{
Function myFunc;
MyClass({this.myFunc = _myDefaultFunc}){...}
static _myDefaultFunc(){...}
}
Or more suitable:
typedef SpecialFunction = ReturnType Function(
FirstParameterType firstParameter,
SecondParameterType secondParameter);
class MyClass{
SpecialFunction myFunc;
MyClass({this.myFunc = _myDefaultFunc}){...}
static ReturnType _myDefaultFunc(FirstParameterType firstParameter,
SecondParameterType secondParameter){...}
}
You can define the default function as private method :
_defaultTransform(Something v) => v;
void _doSomething(List<Something> numbers,
[transform(Something element) = _defaultTransform]) {...}
Or check argument like this :
void _doSomething(List<Something> numbers, [transform(Something element)]) {
if (!?transform) transform = (v) => v;
...
}
Or like Ladicek suggests :
void _doSomething(List<Something> numbers, [transform(Something element)]) {
transform ??= (v) => v;
...
}