gpt4 book ai didi

dart - 如何指定函数参数的默认值?

转载 作者:行者123 更新时间:2023-12-03 02:41:02 25 4
gpt4 key购买 nike

这个函数应该用给定的函数变换来变换列表的每个元素:

void _doSomething(List<Something> numbers, [transform(Something element)]) {...}

因为我不想在 transform 时跳过此方法不应该做任何事情,我想给 transform 一个默认值像这样的方法:

void _doSomething(List<Something> numbers, 
[transform(Something element) = (v) => v]) {...}

不幸的是,编辑告诉我

Expected constant expected



是否有一些解决方法或根本不可能(或根本不应该这样做)?

最佳答案

如果你想初始化一个 Function 参数,它也是你的类的一个字段,我建议:

class MyClass{
Function myFunc;
MyClass({this.myFunc = _myDefaultFunc}){...}
static _myDefaultFunc(){...}
}

或者更适合:
typedef SpecialFunction = ReturnType Function(
FirstParameterType firstParameter,
SecondParameterType secondParameter);

class MyClass{
SpecialFunction myFunc;
MyClass({this.myFunc = _myDefaultFunc}){...}
static ReturnType _myDefaultFunc(FirstParameterType firstParameter,
SecondParameterType secondParameter){...}
}

关于dart - 如何指定函数参数的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com