0

I have a method bool isNotEmpty(dynamic e), where i check nullability myself based on few criteria (e.toString() == null.toString(), e.length for String's and List's, etc.). my question is, I have a piece of code like this:

String e = 'a';
if (isNotEmpty(e)) {
   print(e);
}

how do I let dart be aware that the isNotEmpty method is ensuring that e is not null, and do not have to write print(e!) for every use of the method?

Adnan
  • 906
  • 13
  • 30
  • 2
    Darts flow analysis only works on `local variables` and doesn't work on `class members`. So this might not be possible as of now. – Nisanth Reddy May 15 '21 at 05:54
  • Thanks for the answer, I guess we'll have to wait. – Adnan May 15 '21 at 06:04
  • 1
    Let's see if someone else comes up with something. – Nisanth Reddy May 15 '21 at 06:05
  • 1
    https://dart.dev/null-safety/understanding-null-safety#working-with-nullable-fields – Spatz May 15 '21 at 06:11
  • 1
    @Adnan Check if this answers your question. https://stackoverflow.com/questions/65456958/dart-null-safety-doesnt-work-with-class-fields – Nisanth Reddy May 15 '21 at 06:21
  • I don't think this question is related to "promoting fields". It's still not possible in the current Dart. You cannot link the boolean returned by a function to any property of an argument in such a way that local analysis can then infer that property from the return value. There is simply no functionality to do so. – lrn May 15 '21 at 10:40

0 Answers0