0

I am using an extension on Iterable that adds the following two methods

extension MyIterableExt<T> on Iterable<T> {
  bool get isEmptyOrNull => this == null || this.isEmpty;
  bool get isNotEmptyOrNull => this != null && this.isNotEmpty;
}

This works as I expected, but when I try to use it in statements where the receiver can be null, I get a warning in IntelliJ. For example, the following code works and prints true:

List<String> x;
print('${x?.reversed.isEmptyOrNull}');

Is there any way I can make the Dart Analyzer understand that the extension checks if this == null and thus not show the warning?

Please note that I do not want to have to add suppression directives to every file or line where I use this extension!

I know I can add // ignore_for_file: can_be_null_after_null_aware or // ignore: can_be_null_after_null_aware to make the warning go away, but what I'm looking for is a way to make the Dart Analyzer understand that the warning for this particular extension method is not needed.

This is in a non null-safe project, if that matters.

Magnus
  • 17,157
  • 19
  • 104
  • 189

3 Answers3

1

you can make Iterable Nullable

extension MyIterableExt<T> on Iterable<T>? {
  bool get isEmptyOrNull => this == null || this!.isEmpty;
  bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}
Hooman
  • 753
  • 5
  • 13
0

Make extension base on Iterable? is the way, if you have to find something to disable the warning but not want to change the code then im sorry cause i never do that before.

void main() {
  Iterable? a = [];
  Iterable b = ['1', '2', '3'];
  
  print('a.isEmptyOrNull: ${a.isEmptyOrNull}');
  print('b.isEmptyOrNull: ${b.isEmptyOrNull}');
  print('a.isNotEmptyOrNull: ${a.isNotEmptyOrNull}');
  print('b.isNotEmptyOrNull: ${b.isNotEmptyOrNull}');
}

extension on Iterable? {
  bool get isEmptyOrNull => this == null || this!.isEmpty;
  bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}

result

a.isEmptyOrNull: true
b.isEmptyOrNull: false
a.isNotEmptyOrNull: false
b.isNotEmptyOrNull: true
Tuan
  • 2,033
  • 1
  • 9
  • 16
-1

Based on the Flutter docs, you should be able to add a comment at the top of your code that will essentially disable null safety checks:

// @dart=2.9
import 'src/my_app.dart';

main() {
  //...
}

You should be able to put that comment at the top of any specific file you want this behavior. It doesn't have to be in your "main" file.

triple7
  • 753
  • 4
  • 12
  • I clarified the question a bit. I know I can suppress warnings by putting directives in every file where I use this extension, but that's not what I'm looking for. I want to keep the warnings where they make sense, but for these extension methods the warning doesn't make sense. – Magnus Jun 02 '22 at 08:22
  • I didn't test this, but I would assume that if you add the comment directive to the extension file, it will apply that only to the contents of that file, which should affect any other file using the code within it. – triple7 Jun 02 '22 at 08:48
  • FYI, I didn't downvote your answer, someone else did. But nevertheless, in my opinion it's up to each and everyone to decide for themselves if an answer provides value _in regard to the question_, and up- or downvote as they please. – Magnus Jun 03 '22 at 07:42