我的flutter应用程序正在使用flutter_mailer:^ 0.1.0发送电子邮件。没有附件,它运行良好。但是如果我添加attachments: [ x.path ]
,
此处带有Android控制台的打印输出:
I/flutter ( 8933): /data/user/0/com.gph.testmanual/app_flutter/data.txt
I/fl ( 8933): [/data/user/0/com.gph.testmanual/app_flutter/data.txt]
E/MethodChannel#flutter_mailer( 8933): Failed to handle method call
E/MethodChannel#flutter_mailer( 8933): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.gph.testmanual/app_flutter/data.txt
此处带有IOS控制台的打印输出:
Launching lib/main.dart on iPhone in debug mode...
Starting Xcode build...
Xcode build done.
Installing and launching...
Syncing files to device iPhone...
flutter: /var/mobile/Containers/Data/Application/D87ECFEF-A989-345D-AE543C-1CE5AF96546345F52/Documents/data.txt
我的代码:
class _HomeState extends State<Home> {
File x;
@override
void initState() {
// TODO: implement initState
super.initState();
writeData('Hello, How are you?').then((value) {
x = value;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Read/Write'),
centerTitle: true,
backgroundColor: Colors.greenAccent,
),
body: new Container(
padding: const EdgeInsets.all(13.4),
alignment: Alignment.topCenter,
child:
FlatButton(onPressed: () => _sendEmail(), child: Text("Button"))),
);
}
void _sendEmail() async {
debugPrint(x.path);
final MailOptions mailOptions = MailOptions(
body: 'a long body for the email',
subject: 'Booking',
recipients: ['gph.payment@gmail.com'],
isHTML: false,
// bccRecipients: ['other@example.com'],
// ccRecipients: ['third@example.com'],
// attachments: [ x.path ],
);
await FlutterMailer.send(mailOptions);
}
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path; //home/directory/
}
Future<File> get _localFile async {
final path = await _localPath;
return new File('$path/data.txt'); //home/directory/data.txt
}
//Write and Read from our file
Future<File> writeData(String message) async {
final file = await _localFile;
//write to file
return file.writeAsString('$message');
}
Future<String> readData() async {
try {
final file = await _localFile;
//Read
String data = await file.readAsString();
return data;
} catch (e) {
return "Nothing saved yet!";
}
}
答案 0 :(得分:0)
问题似乎出在应用程序文档目录中-由于某种原因,该扩展名目前还无法共享。 (只需报告一个bug for that。)
我的解决方法是将文件复制到临时目录下,然后从那里附加文件。