Quite new to Flutter so this question might sound obvious but hey… I am trying to use flutter_email_sender (https://pub.dartlang.org/packages/flutter_email_sender) package to attach a .csv file to an email. Here is my code:
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.absolute.path;
}
Future<int> export(String csv) {
final String path = await _localPath + "/" + file_name;
File file = File(path);
file.writeAsString(csv);
File readFile = File(path);
print("content: ${await readFile.readAsString()}");
// the two lines above print the content of the file, as expected
final Email email = Email(
body: 'blah blah',
subject: 'blah blaaaah',
recipients: ["[email protected]"],
attachmentPaths: [file.path],
);
String platformResponse;
try {
print("Trying to send email.");
await FlutterEmailSender.send(email);
platformResponse = 'success';
} catch (error) {
print("Error");
print(error);
platformResponse = error.toString();
}
return platformResponse;
}
And the stack trace:
E/MethodChannel#flutter_email_sender( 3433): Failed to handle method call
E/MethodChannel#flutter_email_sender( 3433): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/package.name/app_flutter/fileName.csv
E/MethodChannel#flutter_email_sender( 3433): at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
E/MethodChannel#flutter_email_sender( 3433): at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
E/MethodChannel#flutter_email_sender( 3433): at com.sidlatau.flutteremailsender.FlutterEmailSenderPlugin.sendEmail(FlutterEmailSenderPlugin.kt:102)
E/MethodChannel#flutter_email_sender( 3433): at com.sidlatau.flutteremailsender.FlutterEmailSenderPlugin.onMethodCall(FlutterEmailSenderPlugin.kt:41)
E/MethodChannel#flutter_email_sender( 3433): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
E/MethodChannel#flutter_email_sender( 3433): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#flutter_email_sender( 3433): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:683)
E/MethodChannel#flutter_email_sender( 3433): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter_email_sender( 3433): at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#flutter_email_sender( 3433): at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#flutter_email_sender( 3433): at android.app.ActivityThread.main(ActivityThread.java:6863)
E/MethodChannel#flutter_email_sender( 3433): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter_email_sender( 3433): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
E/MethodChannel#flutter_email_sender( 3433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/flutter ( 3433): Error
I/flutter ( 3433): PlatformException(error, Failed to find configured root that contains /data/data/package.name/app_flutter/fileName.csv, null)
Note that "/data" appears twice in the path in the error message shown above. I have also tried to set the path manually to remove the additional one, to no avail.
Thank you in advance for your help :)
In:
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.absolute.path;
}
You should instead use:
Future<String> get _localPath async {
final directory = await getExternalStorageDirectory();
return directory.absolute.path;
}
My understanding is as follow, you can read and write in ApplicationDocumentsDirectory but the data can't be accessed outside of the app. Since when you call FlutterEmailSender the data is then sent through an other application, you'll have to save your .csv to a "public" location. If you really want the data to be in the application documents directory, I encourage to investigate further on https://developer.android.com/reference/androidx/core/content/FileProvider But you'll probably have to write a little of platform specific code.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With