【发布时间】:2020-04-03 04:59:24
【问题描述】:
@immutable
abstract class MyGithubReposState extends Equatable {
MyGithubReposState([List props = const []]) : super(props);
}
我在我使用的一个库中看到了上面的代码。 [List props = const []] 是什么意思?道具清单列表?
【问题讨论】:
@immutable
abstract class MyGithubReposState extends Equatable {
MyGithubReposState([List props = const []]) : super(props);
}
我在我使用的一个库中看到了上面的代码。 [List props = const []] 是什么意思?道具清单列表?
【问题讨论】:
在 [] 中包装一组函数参数将它们标记为可选位置参数
String say(String from, String msg, [String device]) {
var result = '$from says $msg';
if (device != null) {
result = '$result with a $device';
}
return result;
}
下面是一个不带可选参数调用这个函数的例子:
assert(say('Bob', 'Howdy') == 'Bob says Howdy');
下面是一个用第三个参数调用这个函数的例子:
assert(say('Bob', 'Howdy', 'smoke signal') ==
'Bob says Howdy with a smoke signal');
【讨论】:
这是可选参数,如下所述。
首先列出必需的参数,然后是任何可选参数。可选参数可以命名或定位。
命名参数
调用函数时,可以使用 paramName: value 指定命名参数。例如:
这是函数调用
enableFlags(bold: true, hidden: false);
定义函数时,使用 {param1, param2, …} 指定命名参数:
这就是我们定义它们的方式
/// Sets the [bold] and [hidden] flags ...
void enableFlags({bool bold, bool hidden}) {...}
位置参数
在 [] 中包装一组函数参数将它们标记为可选的位置参数:
String say(String from, String msg, [String device]) {
var result = '$from says $msg';
if (device != null) {
result = '$result with a $device';
}
return result;
}
这样我们就可以通过两种方式调用这个函数
没有可选的位置参数
say('Bob', 'Howdy')
带有可选位置参数
say('Bob', 'Howdy', 'smoke signal')
【讨论】:
[within this is optional] 表示这些参数是可选的
【讨论】: