How to put a conditional in json post request flutter

Viewed 88

I have this payload that I need to send to a server

"members": [
 {
 "names": "ben",
 "date-of-birth": "1978-01-01",
 "gender": "Male",
 "surname": "surname",
 "role": "Partner",
 "total-cut": "100.00"
 }
 ],

Only thing is at times there are no members, and following this am not supposed to send the array at all, it should just be nothing at all, no members. For clarification, this is an example only, think there is a members object, like the above, schools object, courses object, only at times some of this come up empty and consequently I should omit the empty object entirely. For example, in the below, if there are no members,,

{
"members": [
 {
 "names": "ben",
 "date-of-birth": "1978-01-01",
 "gender": "Male",
 "surname": "surname",
 "role": "Partner",
 "total-cut": "100.00"
 }
 ],
"courses": [
 {
 "name": "ben",
 "number": "32",
 "teacher": "Russ",
 "cut": "10.00"
 }
 ],
}

how can i create a conditional that omits the members and leaves courses only

{
"courses": [
 {
 "name": "ben",
 "number": "32",
 "teacher": "Russ",
 "cut": "10.00"
 }
 ],
}

For context this is a post request

3 Answers

I don't know if this has been solved or not yet (I hope yes :P). But this is a practical approach for reference in case others do run into a similar issue.

Problem

Before, here is a problem rephrasing just to make sure we are on the same line. If you have members, add them to the map otherwise no. In both these conditions the map should look like this:

// With members
{
  "members": [
     {
       "names": "member_name",
       //...
     }
   ],
  "courses": [
     {
       "name": "course_name",
       //...
     }
   ],
}

// Without members
{
  "courses": [
     {
       "name": "course_name",
       //...
     }
   ],
}

Solution

In my opinion, the best way to handle this is to declare an empty Map() and conditionally add entries to it as fellows:

Map<String, dynamic> buildMyMap(){
  final buffer = <String, dynamic>{};
  if(members.isNotEmpty){
    // Option 1
    buffer.addEntries(MapEntry("members", members));
    // Option 2
    buffer["members"] = members;
  }else{
    // (Optional) In case you want to delete pre-existing members
    buffer.remove("members");
  }
  if(courses.isNotEmpty){
    // Option 1
    buffer.addEntries(MapEntry("courses", courses));
    // Option 2
    buffer["courses"] = courses;
  }else{
    // (Optional) in case you want to remove pre-existing courses!
    buffer.remove('courses');
  }

  return buffer;
}

You should declare the parameter members to be optional in your API, then you have no need to send this parameter.

you can do the following

final List members = [];
final List courses = [];
final map = {
  if (members.isNotEmpty)
    'members': [
      for (final member in members)
        {
          "names": "ben",
          "date-of-birth": "1978-01-01",
          "gender": "Male",
          "surname": "surname",
          "role": "Partner",
          "total-cut": "100.00"
        }
    ],
  if (courses.isNotEmpty)
    'courses': [
      for (final course in courses)
        {
          "name": "ben",
          "number": "32",
          "teacher": "Russ",
          "cut": "10.00"
        }
    ]
};

you can use if statement inside a map or a list in flutter, also, if you want to multiple fields if a condition is met

final map2 = {
  if(true)...{
    'name': 'name',
    'age': '12'
  } else ...{
    'name': 'NO NAME',
    'age': 'NO AGE'
  }
};
Related