skip to Main Content

I hope you can help me, I have this json to send in a request

{
  "taxpayer": _person,
  "customerType": _dropdownTypeClient,
  "rfc": _rfc.text,
  "name": _name.text,
  "fatherSurname": _firstName.text,
  "motherSurname": _lastName.text,
  "businessName": _razonSocial.text,
  "contact": _contact.text,
  "phone": _phone.text,
  "cell": _cellphone.text,
  "taxRegime": _dropdownTax,
  "mailList": [
    {"mail": _email.text},
    {"mail": _emailTwo.text}
  ],
  "workType": _dropdownWorkType,
  "howToContact": _dropdownHowToContact
}

but i want to send the mailList as a real List, because th emailTwo is optional and when I send the information the DB counts the empty value. How i do the converse ? I tried this for simplicity but it didnt work.

//List mails = [];

//mails.add(_email.text);

//if (_emailTwo.text.isNotEmpty) {
//mails.add(_emailTwo.text);
//}

2

Answers


  1. You can use a simple if inside the map literal, like so:

    {
      "mailList": [
        {"mail": _email.text},
        if (_emailTwo.text.isNotEmpty)
          {"mail": _emailTwo.text},
      ]
    }
    
    Login or Signup to reply.
  2. It is impossible to know exactly why is your attempt "not working" without you giving the error messages. But we can guess, maybe you should try this:

    List<dynamic> mails = [];
    
    mails.add({"mail":_email.text});
    final email2 = _emailTwo.text;
    if (email2.isNotEmpty) {
      mails.add({"mail":email2 });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search