0

我使用颤振表单生成器获取带有相机/图库的图像,然后我想将它们附加到电子邮件中(我为此使用邮件程序)但是当我尝试发送电子邮件时出现以下错误:

type 'List< dynamic >' is not a subtype of type 'List< Attachment >'

在此处输入图像描述 有没有办法将动态列表转换为附件列表?

这是我的代码:

我使用 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>();

但现在我收到以下错误: 在此处输入图像描述

4

2 回答 2

2

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

//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();

显然,使代码适应您的数据。要更好地了解这些类的结构,请查看此页面:附件类 github 参考

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

于 2020-12-01T15:04:24.223 回答
0

我认为您应该传递List<dynamic>附件而不是List<Attachment>. 试试看。

于 2020-12-02T02:09:19.083 回答