【问题标题】:type 'List<dynamic>' is not a subtype of type 'List<Attachment>''List<dynamic>' 类型不是 'List<Attachment>' 类型的子类型
【发布时间】:2020-12-01 14:18:46
【问题描述】:

我使用flutter form builder 使用相机/图库获取图像,然后我想将它们附加到电子邮件中(我为此使用mailer)但是当我尝试发送电子邮件时,我收到以下错误:

type 'List&lt; dynamic &gt;' is not a subtype of type 'List&lt; Attachment &gt;'

有没有办法将动态列表转换为附件列表?

这是我的代码:

我使用 FormBuilderImagePicker 小部件获取图像:

FormBuilderImagePicker(
 attribute: 'image',
 onChanged: (value) {
  _attachments.add(value);
  debugPrint('attachments: $_attachments');
},
maxImages: 3,
defaultImage:
AssetImage('assets/images/icons8-plus-64.png'),
validators: [
 FormBuilderValidators.required(),
],
),

这是我发送电子邮件的代码:

  void sendEmailWithoutPosition(
  String name,
  String destination,
  String subject,
  String description,
  String email,
  String number,
  List<Attachment> attachments,
  BuildContext context) async {
message = Message()
  ..from = Address(username, name)
  ..recipients.add(destination)
  ..subject = ' Petiție ${subject.toString()} - aplicația e-Rădăuți'
  ..html = 'Către, ${destination.toString()} <br><br> Stimată doamnă/ Stimate domn,'
      '<br><br>Subsemnatul ${name.toString()}, vă supun atenției următoarea problemă:<br><br>'
      '$description<br><br>În conformitate cu atribuțiile pe care le aveți, vă rog să luați'
      ' măsurile ce se impun.<br><br>'
      'Prezenta sesizare reprezintă o petiție în sensul O.G. nr. 27/2002 privind activitatea de soluționare a petițiilor și '
      'a fost transmisă prin intermediul aplicației mobile e-Rădăuți, dezvoltată'
      ' de Ascociația Rădăuțiul Civic, prin funcționalitatea „Sesizează o problemă”.<br><br>'
      'Vă rog să îmi transmiteți răspunsul în termenul legal la adresa $email'
      '.<br><br>Cu stimă,<br><br>'
      '     $name<br><br>     Tel: $number/$email'
  ..attachments.addAll(attachments);
tryToSendEmail(message);
}

Future<bool> tryToSendEmail(var message) async {
final smtpServer = gmail(username, password);
try {
  final sendReport = await send(message, smtpServer);
  debugPrint('Message sent: ' + sendReport.toString());
} on MailerException catch (e) {
  print('Message not sent.');
  for (var p in e.problems) {
    print('Problem: ${p.code}: ${p.msg}');
  }
}
}

我尝试使用此代码映射列表:

List<Attachment> listAttachments = _attachments
                            .map((e) => e as Attachment)
                            .toList();
                        List<Attachment> listAttachmentsNew =
                            listAttachments.cast<Attachment>();

但现在我收到以下错误:

【问题讨论】:

  • 仅看stacktrace很难理解问题所在。你能给我们一些代码来更好地理解你的问题的范围吗?
  • 哦,我的大部分代码都来自 2 个包中的文档,但我肯定会发布代码
  • 我认为最相关的部分是当您执行 List something = somethingElse。这是为了了解“somethingElse”是什么,并查看是否可以将该列表的内容映射到附件列表
  • @AndreaCostanzo1 我已经添加了我尝试映射列表的方式以及我得到的错误
  • 你不能简单地转换,因为附件是一个抽象类。您必须使用构造函数正确创建附件。我在下面发布了一个答案,让您更好地了解应该如何处理这个问题

标签: flutter email-attachments mailer dynamic-list


【解决方案1】:

通过深入研究包的结构,我建议您使用构造函数:附件是一个抽象类,不能简单地强制转换。根据您可以执行的附件类型:

//FOR FILE ATTACHMENTS
List<Attachment> listAttachments = _attachments.map((e) => FileAttachment(e.file)).toList();
//FOR STRING ATTACHMENTS
List<Attachment> listAttachments = _attachments.map((e) => StringAttachment(e.data)).toList();

显然要根据您的数据调整代码。 要更好地了解这些类的结构,请查看此页面:Attachment class github reference

更新:我查看了您正在使用的库,但它缺少文档。使用 onChanged 尝试使用 onImage。这样,每次选择图像时,您都应该获得所需的数据。

【讨论】:

  • 它说:未处理的异常:NoSuchMethodError:类'_File'没有实例获取器'file'。
  • 好吧 luis,如果“e”已经是一个文件,你必须做 (e) => FileAttachment(e)。如果您向我提供有关“e”属于哪个类的更多信息,我可以更新上面的代码以使其适用于您的特定情况
  • 如果我只是简单地输入“e”,我会得到同样的错误:未处理的异常:类型 'List' 不是类型 'List' 的子类型 Mailer 想要一个 FileAttachment 但FormBuilderImagePicker 返回一个 List 并且不知何故我必须将返回的列表从 ImagePicker 转换为附件类型。哦顺便说一句 FormBuiler 使用 pub.dev/packages/image_picker" 来获取图像
  • Luis,您使用的包的类缺少文档。我试图查看代码,但有时它会变得混乱。它有一个 onImage 方法,但似乎没有被调用。尝试使用该方法,否则在他们的 github 页面上打开一个问题,询问该特定类的解释,因为从文档中不清楚。
  • 好的,谢谢,但我已经做了类似这样的事情 File file1 = FileAttachments(attachments[1]);和 listAttachments = [file1];对于 3 张图片,我知道这不是什么好东西,但是现在可以使用 PS 附件属于类型 List 并且 listAttachments 属于 List 类型
【解决方案2】:

我认为您应该传递List&lt;dynamic&gt; 附件而不是List&lt;Attachment&gt;。试试看。

【讨论】:

  • 我在发布这个问题之前试过这个,但是谢谢
猜你喜欢
  • 1970-01-01
  • 2021-12-29
  • 2023-01-08
  • 2021-06-24
  • 2021-07-06
  • 2021-02-17
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多