DartPad 一直说期待 2,在构造函数中接收 3 个参数

DartPad keeps saying expecting 2, receiving 3 arguments in constructor

Email( List<String> to, String from, {String subject:null, String message:null, DateTime datetime:null, HashMap<String, List<String>> meta:null, List<Attachment> attachments:null})

得到这个错误:

Uncaught NoSuchMethodError: incorrect number of arguments passed to method named ''
Receiver: ""
Tried calling: (Instance of 'JSArray<String>', "will@fallenreaper.com", Instance of '_HashMap<String, Object>')
Found: (to, from, subject, message, datetime, meta, attachments)

我的class是:

class Email{
  Email( List<String> to, String from, {String subject:null, String message:null, DateTime datetime:null, HashMap<String, List<String>> meta:null, List<Attachment> attachments:null}){
    //do stuff.
  }
}

我试图让它成为必需的来回,但其余的可以选择在地图中传入。我以为我这样做是对的,但它似乎不正确。

DartPad 位于:https://dartpad.dartlang.org/4d32b88095a6509da511

搜索 TODO

作曲家 class 是我实施的部分,利用我创建的电子邮件 class。

向主题添加一些文本,然后单击 "Send" 按钮

你的 Email class' 构造函数

Email( List<String> to, String from, {String subject:null, String message:null, DateTime datetime:null, HashMap<String, List<String>> meta:null, List<Attachment> attachments:null}) { ... }

有 2 个位置参数和 5 个可选的命名参数。

您可以像在

中那样简单地传递位置参数
new Email(map["to"], map["from"], map);

但可选名称参数需要 "named" 如

new Email(map["to"], map["from"], subject: map);

如果您希望可选参数是位置性的,您需要将构造函数更改为

Email( List<String> to, String from, [String subject=null, String message=null, DateTime datetime=null, HashMap<String, List<String>> meta=null, List<Attachment> attachments=null]) { ... }

您不能同时拥有可选的命名 可选的位置参数。如果你有一个较长的参数列表,比如在你的 Email 构造函数中,命名可选参数通常更好,因为你可以用一个名称指定传递的值应该分配给哪个参数,而不需要传递 nulls 或 ''s 表示要跳过的参数。