Flutter - message is only sent if all fields are filled

Asked

Viewed 50 times

-3

When I try to send a message without an image, the Mailer shows an error, but if I take the field ..attachments.add(new Fileattachment(_imagemAn,)); the message is sent normally without presenting the error, however, the funny thing is that I am not getting it, if in that same message that was giving error by being sent without image, containing ..attachments.add(new Fileattachment(_imagemAn,));, just add an image and the message is sent.

it’s almost as if I was forced to send with image, I need to say that ..attachments.add(new Fileattachment(_imagemAn,)); equal to or different from nil?

I’m sweating the Mailer API: 4.0.0.

More directly, when the message is sent only if all fields are filled in, if one of the fields is not filled in, how to correct?

This is the code I’m using, this is the screen where the message is displayed before sending, as a confirmation page before sending.

class PaginadeBoasVindasAN extends StatelessWidget {

  String _localAn;
  String _problemaAn;
  File _imagemAn;
  File _imagemGaleriaAn;

  PaginadeBoasVindasAN(
    this._localAn,
    this._problemaAn,
    this._imagemAn,
    this._imagemGaleriaAn,
  );

  // Aqui vamos criar o e-mail - smtp

  enviarMensagem() async {
    String username = '@gmail.com';
    String password = '@#';

    final smtpServer = gmail(username, password);
    
    final message = Message()
      ..from = Address('Sem identificação', ' ')
      ..recipients.add('@gmail.com')
      ..ccRecipients.addAll(['[email protected]', '[email protected]'])
      ..bccRecipients.add(Address('[email protected]'))
      ..subject = '$_localAn :: ${DateTime.now()}'
      ..text = 'LOCAL: $_localAn,n\: $_problemaAn'
      ..attachments.add(FileAttachment(_imagemAn,))
      ..attachments.add(FileAttachment(_imagemGaleriaAn,));


    try {
      final sendReport = await send(message, smtpServer);
      print('Mensagem enviada: ' + sendReport.toString());
    } on MailerException catch (e) {
      print('Mensagem não enviada.');
      for (var p in e.problems) {
        print('Problema: ${p.code}: ${p.msg}');
      }
    }
    var connection = PersistentConnection(smtpServer);

    await connection.send(message);

    await connection.close();
  }
  • 1

    Your question is a little confusing, but come on... If you want the image to be optional for sending, you need to make a condition to send or not the same, for example if (_imageAn != null) message.FileAttachment(_imagemAn,). Otherwise do not even call this function, because the FileAttachment will try to create something that does not exist.

  • I’m sorry for the confusion in the message, but you presented me something that I believe will work, I’m new and studying on my own, with I would implement this condition to my code?

1 answer

2


As you are starting, I leave as a hint, that go a little at a time, try to understand a little more about some points of the framework and language.

In your code, just process the image data to know whether or not to inform it, do the following:

class PaginadeBoasVindasAN extends StatelessWidget {

  String _localAn;
  String _problemaAn;
  File _imagemAn;
  File _imagemGaleriaAn;

  PaginadeBoasVindasAN(
    this._localAn,
    this._problemaAn,
    this._imagemAn,
    this._imagemGaleriaAn,
  );

  // Aqui vamos criar o e-mail - smtp

  enviarMensagem() async {
    String username = '@gmail.com';
    String password = '@#';

    final smtpServer = gmail(username, password);
    
    final message = Message()
      ..from = Address('Sem identificação', ' ')
      ..recipients.add('@gmail.com')
      ..ccRecipients.addAll(['[email protected]', '[email protected]'])
      ..bccRecipients.add(Address('[email protected]'))
      ..subject = '$_localAn :: ${DateTime.now()}'
      ..text = 'LOCAL: $_localAn,n\: $_problemaAn';

     if ((_imageAn != null) && (_imageAn != ""))
       message.attachments.add(FileAttachment(_imagemAn,));
     if ((_imagemGaleriaAn!= null) && (_imagemGaleriaAn!= ""))
       message.attachments.add(FileAttachment(_imagemGaleriaAn,));


    try {
      final sendReport = await send(message, smtpServer);
      print('Mensagem enviada: ' + sendReport.toString());
    } on MailerException catch (e) {
      print('Mensagem não enviada.');
      for (var p in e.problems) {
        print('Problema: ${p.code}: ${p.msg}');
      }
    }
    var connection = PersistentConnection(smtpServer);

    await connection.send(message);

    await connection.close();
  }

Explanation

Your code fails because you try to instill a FileAttachment() with an invalid or even NULL path.

Info

Use the .. just makes your code easier, so you don’t have to keep doing it all the time

message.metodoX()
message.metodoY()
message.metodoZ()

Can only do

message = Message()
..metodoX()
..metodoY()
..metodoZ()
  • Matheus, thank you! Literally saved me, not only with a direction to study, but also helping me with this problem of mine!

  • I bought a course at Udemy to learn the language, but although I learned Dart, I have difficulty using the language within the framework in some situations, this was one of them.

  • 1

    I am happy to have helped, if the answer served you (and if you do not want to wait others) please mark the answer as accepted.

  • Again, thank you!

  • Good night Matheus, how are you? Sorry to bother you, but I have a problem in that area of the code where they were added the if (in the code you gave me ), out of nowhere it started to return from the following two errors: Exception has occurred. _Assertionerror (Failed assertion: Boolean Expression must not be null) and the other error is this Exception has occurred.Smtpmessagevalidationexception (Invalid message..

  • I have tried everything, but I have not succeeded in anything I do to try to solve the problem, nor have I found anyone with similar problem, something to talk about in the library and etc...

  • 1

    These are problems related to trying to put an incorrect value in a variable, you may have changed something without noticing, review your code. Not much to do with the conditional (IF)

  • Thanks Matheus! I could not find the incorrect value , but as I had backed up the project I was able to continue!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.