flutter - 为什么要指明Option参数?

标签 flutter dart

我想创建一个 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/

相关文章:

ios - Flutter 无法在 VS Code 上构建示例 iOS 应用程序

firebase - Firestore更新子集合文档

android - Flutter:任务 ':app:compileDebugKotlin' 执行失败

Flutter - CupertinoActionSheet/CupertinoActionSheetAction 背景颜色在设备上与模拟器中不同

android - Dart OAuth 1.0 - 无效签名 - 提供的签名不匹配

memory-leaks - Dart/Flutter 有弱引用的概念吗?

flutter - 如何在 Flutter 中添加带有图标的按钮

android - Webview 中的全屏视频在 Flutter 中不起作用

flutter - http post 时出现 SocketException : OS Error: Connection refused, errno = 111,地址 = localhost,端口 = 37312

dart - 如何从 Dart 编辑器配置 dart2js 选项?