Print keys were values is null in map<String,dynamic> Employee in dart

Issue

This is the code json code from which I want to get all the keys were value is null

{
   "service": "register",
   "employee": {
       "employeeId": "bcvfevse",
       "officeId": null,
       "email": null,
       "name" : "Chetan Patil",
       "position" : "Flutter Developer",
       "number" : null
   },
   "device": {
       "type": "android",
       "uniqueId": "9774d56d682e549c"
   }
}

I Want something like this ["number","email","officeId"] in "DART"

Solution

If you have something like this:

var json = {
   "service": "register",
   "employee": {
       "employeeId": "bcvfevse",
       "officeId": null,
       "email": null,
       "name" : "Chetan Patil",
       "position" : "Flutter Developer",
       "number" : null
   },
   "device": {
       "type": "android",
       "uniqueId": "9774d56d682e549c"
   }

And in Dart you do something like:

var nullEntries = (json['employee'] as Map<String, dynamic>).entries.where((e) => e.value == null).toList();

Then if you print it out like this:

print(nullEntries.map((p) => p.key));

You get:

(officeId, email, number)

Let me know if that’s what you’re looking for.

Answered By – Roman Jaquez

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *