3

Is there any decent way of checking if object has all null values? Here is example code.. I'm trying to print 'empty' when all object values are null

void main() {

  Object object = Object(foo: null, fuu: null);

  object == null? print('empty'): print('not empty');
 }

 class Object {
   final String foo;
   final int fuu;
   Object({this.foo, this.fuu});
 }
LonelyWolf
  • 4,164
  • 16
  • 36

1 Answers1

0
void main() {

  Object objectvalue = Object();


  objectvalue.foo = null;
  objectvalue.fuu = 21;


  if(objectvalue.checknullValue()){
    print("some fields are null");
  }
 }

 class Object {
    String foo;
    int fuu;
   Object({this.foo, this.fuu});

   bool checknullValue() {
    return [foo, fuu].contains(null);
  }
 }


check out this code and let me know if it works.

Thanks.

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34
  • Is there you way not to list all fields in checknullValue method? – LonelyWolf Jan 26 '20 at 14:49
  • its on you what you want to include – Sagar Acharya Jan 26 '20 at 14:51
  • Thing is I've got a lot of fields in the object and list them into the array is a bit pain. Also if I will want to change some of the fields then I would have to change it in the array... Not exactly decent way but it works. Thanks – LonelyWolf Jan 26 '20 at 14:55
  • Not sure but if this link can help you. https://stackoverflow.com/questions/42446566/dart-null-false-empty-checking-how-to-write-this-shorter – Sagar Acharya Jan 26 '20 at 15:08
  • 1
    not exactly... They are checking if one of the field is empty null or false. My problem with your solution is that I have to write all fields into the array which btw check if any of the field is null instead all fields are null. Basically I've ended up with long if statement where I check each field for null. – LonelyWolf Jan 26 '20 at 16:10
  • 1
    You're just adding a manual validation. This is not what @LonelyWolf asked as "decent way" in my opinion – Ale TheFe Oct 22 '21 at 10:22