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

