我想创建一个 Setter() 并找到使用 newValue 的 Option 参数的代码。 这应该被解释为类似于括号中的 [optional parameter] 吗?它也可以完全排除吗?
class SomeState with ChangeNotifier {
int _progress = 0;
Option _selected;
final PageController controller = PageController();
get progress => _progress;
get selected => _selected;
set progress(double newValue) {
_progress = newValue;
notifyListeners();
}
set selected(Option newValue) { //here
_selected = newValue;
notifyListeners();
}
}
最佳答案
不确定我是否清楚地理解了您的问题。
<强>1。必需参数
// here you need to provide Option
set selected(Option newValue) {
_selected = newValue;
notifyListeners();
}
你会像这样使用它
selected(value);
<强>2。位置参数
// here Option is optional
set selected([Option newValue]) {
_selected = newValue;
notifyListeners();
}
你会像这样使用它
selected(value); // if you need to provide value
selected(); // if you don't want to provide any value
<强>3。命名参数
// here Option is optional
set selected({Option newValue}) {
_selected = newValue;
notifyListeners();
}
你会像这样使用它
selected(value: value); // if you want to provide value
selected(); // if you don't to provide value
注意:
如果您在变量前使用@required
,位置和命名参数也可以成为必需的。
如果你想给可选参数任何默认值,你可以在参数中使用以下
set selected({Option newValue = const SomeValue()}) {...}
关于flutter - 为什么要指明Option参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57691529/