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


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 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

Composer 类是 im 实现的部分,利用我创建的电子邮件类。

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

你的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}) { ... }

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

您可以像在

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

但是可选名称参数需要像

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 构造函数一样具有较长的参数列表,则命名可选参数通常更好,因为您可以使用名称指定传递的值应分配给哪个参数,并且无需为要跳过的参数传递 null s 或 '' s。

相关内容

最新更新