@immutable
abstract class MyGithubReposState extends Equatable {
MyGithubReposState([List props = const []]) : super(props);
}
我在我使用的库之一中看到了上面的代码。
[List props = const []]
有什么用意思? Prop list 一览?
最佳答案
这是可选参数,如下所述。
需要 和 可选 .
首先列出必需的参数,然后是任何可选参数。可选参数可以是命名的或位置的。
命名参数
调用函数时,您可以使用 paramName: value 指定命名参数。例如:
this is calling of function
enableFlags(bold: true, hidden: false);
定义函数时,使用 {param1, param2, ...} 指定命名参数:this is how we define them
/// 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;
}
so that we can call this function by two way
没有可选的位置参数
say('Bob', 'Howdy')
带有可选的位置参数 say('Bob', 'Howdy', 'smoke signal')
Reference here
关于flutter - dart中构造函数中的括号是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59259913/