0

I am reporting the following error while executing the code below

bool isNotEmpty(String text) {
  return text?.isNotEmpty ?? false;
}

bool isEmpty(String text) {
  return text?.isEmpty ?? true;
}

lib/util/string_util.dart:2:10: Warning: Operand of null-aware operation '?.' has type 'String' which excludes null. return text?.isNotEmpty ?? false;

lib/util/string_util.dart:6:10: Warning: Operand of null-aware operation '?.' has type 'String' which excludes null. return text?.isEmpty ?? true;

enter image description here

enter image description here

ruofeng
  • 11
  • 1
  • Does this answer your question? [Why im getting this error Warning: Operand of null-aware operation '??' has type 'Color' which excludes null](https://stackoverflow.com/questions/67389956/why-im-getting-this-error-warning-operand-of-null-aware-operation-has-type) – Bishan Jul 26 '21 at 03:53
  • why you are using `??` operator ? just simple `text.isEmpty` will work ! because you defined your text variable as `String` not `String?` so the text variable cannot be `null`, so you need not to include `?` after text as it cannot be null. – Aayush Shah Jul 26 '21 at 03:54

1 Answers1

0

Try this one

  bool isNotEmpty(String? text) {
    if (text == null) {
      text = "";
    }
    return text.isNotEmpty;
  }

  bool isEmpty(String? text) {
    if (text == null) {
      text = "";
    }
    return text.isEmpty;
  }

More detail

Sam Chan
  • 1,665
  • 4
  • 14