Home>

Flutter uses a real Android device to create a CSV file and attach it by email.
At that time, the csv file will be spit out to the current directory of the Android device.
It looks like this is n’t working.

When the following function is executed on Windows 10, a CSV file is generated properly.
If it is an Android device, it will not work.

Error message
E/MethodChannel # flutter_mailer (17274): Failed to handle method call
E/MethodChannel # flutter_mailer (17274): java.lang.IllegalArgumentException: Failed to find configured root that contains /sample.csv
E/MethodChannel # flutter_mailer (17274): at androidx.core.content.FileProvider $SimplePathStrategy.getUriForFile (FileProvider.java:739)
E/MethodChannel # flutter_mailer (17274): at androidx.core.content.FileProvider.getUriForFile (FileProvider.java:418)
E/MethodChannel # flutter_mailer (17274): at com.dataxad.fluttermailer.FlutterMailerPlugin.mail (FlutterMailerPlugin.java:116)
E/MethodChannel # flutter_mailer (17274): at com.dataxad.fluttermailer.FlutterMailerPlugin.onMethodCall (FlutterMailerPlugin.java:50)
E/MethodChannel # flutter_mailer (17274): at io.flutter.plugin.common.MethodChannel $IncomingMethodCallHandler.onMessage (MethodChannel.java:222)
E/MethodChannel # flutter_mailer (17274): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart (DartMessenger.java:96)
…</Code></pre>
<strong>Applicable source code</strong>
<pre><code>// Function to create a file
import 'dart: async';
import 'dart: io';
import 'package: flutter/material.dart';
import 'package: flutter_mailer/flutter_mailer.dart';
void main () =>runApp (new MyApp ());
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState () =>new _MyAppState ();
}
class _MyAppState extends State<MyApp>{
  final GlobalKey<ScaffoldState>_scafoldKey = GlobalKey<ScaffoldState>();
  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void>send () async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    final MailOptions mailOptions = MailOptions (
      body: null,
      subject: 'attach file',
      recipients: [''],isHTML: true,
      // bccRecipients: ['[email protected]'],


      ccRecipients: [''],


      attachments: [_createFile (). path],


    );
    String platformResponse;
    try {
      await FlutterMailer.send (mailOptions);
      platformResponse = 'success';
    } catch (error) {
      platformResponse = error.toString ();
    }
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (! mounted) return;
    _scafoldKey.currentState.showSnackBar (SnackBar (
      content: Text (platformResponse),
    ));
  }
  @override
  Widget build (BuildContext context) {
    return new MaterialApp (
      theme: ThemeData (primaryColor: Colors.red),
      home: new Scaffold (
        key: _scafoldKey,
        appBar: new AppBar (
          title: const Text ('Plugin example app'),
          actions:<Widget>[
            IconButton (
              onPressed: send,
              icon: const Icon (Icons.send),)
          ],


        ),
        body: SingleChildScrollView (
          child: new Center (
            child: Padding (
              padding: const EdgeInsets.all (8.0),
              child: Column (
                mainAxisSize: MainAxisSize.max,
                // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
              ),
            ),
          ),
        ),
      ),
    );
  }

// Function to create a file
  File _createFile () {
    Directory current = Directory.current;
    String fileName = current.path + "\ sample.csv";
    print (fileName);
    File file = new File (fileName);
    file.writeAsString ("Hello World !!");
    return file;
  }
}

When you execute and press the mail button to call send, I want the attachment to have an attached file.
If i print in the function, it will be "\ sample.csv" and it is unknown whether Directory current is returned properly.

Supplemental information (FW/tool version etc.)

Windows10, Android version 5.1

  • Answer # 1

    Self-solved. I think that this code probably disappeared because of the scope, because the file was created in another class and the call was insufficient.
    I was able to use a library called path_provider.