在dart中:

命名参数的功能类似于-

String send(msg, {rate: 'First Class'}) {
  return '${msg} was sent via ${rate}';
}

// you can use named parameters if the argument is optional
send("I'm poor", rate:'4th class'); // == "I'm poor was sent via 4th class"

简写构造函数参数的功能如下-

class Person {
  String name;

  // parameters prefixed by 'this.' will assign to
  // instance variables automatically
  Person(this.name);
}



Is there any way to do something like the below?-

class Person{
   String name;
   String age;

   Person({this.name = "defaultName", this.age = "defaultAge"});
}

//So that I could do something like:
var personAlpha = new Person(name: "Jordan");

谢谢

dartlang synonymsborrow 的代码示例

推荐答案

Update

是的,DART 2中允许使用=,现在优先使用=而不是:,以匹配可选的位置参数.

Person({this.name = "defaultName", this.age = "defaultAge"});

老答案

您只需使用冒号而不是等号

class Person {
   String name;
   String age;

   Person({this.name: "defaultName", this.age: "defaultAge"});
}

我发现这仍然令人困惑,可选参数使用=来指定默认值,但命名为Use :. 应该问问自己.

Dart相关问答推荐

无法在 IOS 上使用 firebase 运行Flutter应用程序

Dart 包对 git repo 子目录的依赖

Flutter Drawer 作为单独的小部件不允许修改宽度

flutter: 使用提供程序时,状态在热重载时丢失

在 Flutter 中检测键盘事件

Flutter - 为什么滑块不会在 AlertDialog 中更新?

Flutter插件和Flutter模块之间有什么区别?

Flutter Drawer Widget - 更改 Scaffold.body 内容

Flutter:我应该在哪里调用 SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark)

Flutter类继承

Flutter-键盘显示和隐藏导致生成调用

如何在 Dart 中获取当前脚本的目录?

你如何在 Dart 中将月份的日期格式化为11th、21st或23rd?

如何在dart中的多个文件中编写多个单元测试?

无法使用 Flutter 调用 Localhost,将随机端口分配给 HTTP GET 调用

轻松判断数字是否在 Dart 的给定范围内?

Dart 中的异步可迭代映射

在 Dart 中创建具有可变数量的参数或参数的函数

Dart lambda/shortland 函数混淆

Angular.js 和 Angular.dart 的区别?