【发布时间】:2021-07-20 01:40:02
【问题描述】:
出于测试目的,能够在执行之前为函数“准备”参数是很有用的,这样可以根据任何结果检查参数。
在 JavaScript 中我可以这样做:
function testFunc ({id, count}) {
/* perform some operation */
return {id, count}
}
const args = {
id: 'someId',
count: Math.round(Math.random() * 10),
}
const res = testFunc({...args})
/* check that count is correct etc */
如何在 Dart 中实现同样的灵活性?
Map<String, dynamic> testFunc({String id, int count}) {
/* perform some operation */
return {
'id': id,
'count': count,
};
}
final args = /* erm? */
testFunc(args); /* hmmm? */
我是否试图突破强类型语言的极限?
【问题讨论】:
标签: flutter dart parameters parameter-passing