【发布时间】:2019-03-04 01:36:45
【问题描述】:
我的 Flutter 应用正在使用 flutter_mailer: ^0.1.0 发送电子邮件。它在没有附件的情况下运行良好。但是如果我添加attachments: [ x.path ],
- 对于 IOS,当我按下按钮时,应用程序将立即退出。
- 对于 Android,当我按下按钮时,应用程序不会打开电子邮件应用程序。 请帮忙解决这个问题。
这里是 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!";
}
}
【问题讨论】: