Null Safety firstWhere

The change to null safety in Dart is a good thing, however a common pattern I used firstWhere for got broken. The pattern is to search a collection and return null if the search returns nothing. The short answer is that you should use firstWhereOrNull instead in this case.

Example of the Issue

Before the null safe version of Dart the code would look like this:

List<int> testData = [1,2,3];

int val = testData.firstWhere((element) => element == 4, orElse: () => null);
if ( null == val ) {
  print('Not found.');
}

The naive conversion to null safety would be:

List<int> testData = [1,2,3];

int? val = testData.firstWhere((element) => element == 4, orElse: () => null as int);
if ( null == val ) {
  print('Not found.');
}

But this conversion will show a warning that the test if ( null == val ) is not required because the value can’t be null. What’s happening? The return type for the firstWhere method is T not T? so it can’t return a null. Instead it will throw if the element is not found. There’s a good discussion here if you’re interested. Note that this is a warning, not an error so it can be a bit subtle to diagnose.

Correct Conversion

import 'package:collection/collection.dart'; // <- required or firstWhereOrNull is not defined

void main() {
  List<int> testData = [1,2,3];

  int? val = testData.firstWhereOrNull((element) => element == 4);
  if ( null == val ) {
    print('Not found.');
  }
}

This is actually a pretty nice solution as the orElse condition is no longer required.

Leave a Reply