I have upgraded my Flutter project to newer Dart SDK version. I have an util that can return null object, it looks like this (example):
static Item? getCurrentItem(List<Item> list, DateTime now) {
return list.firstWhereOrNull((item) {
return ItemUtils.hasItemStartedStarted(item, now) &&
!ItemUtils.didItemFinish(item, now);
});
}
This static method returns null in one instance and later on when I try to do this:
final item = ItemUtils.getCurrentItem(items, now);
if (item != null && ItemUtils.someOtherMethod(item, now) {
...
}
The code runs inside StreamBuilder
repeatedly, I have a Timer
set up that re-renders it periodically.
It throws error Null check operator used on a null value
. I checked value of item
variable and it is null
. But item != null
reports false, why? I checked this inside debugger, is it possible it's some stale data because of StreamBuilder
?
This code worked before. The migration tool used firstWhereOrNull
method instead of .firstWhere(...., orElse: () => null)
which I'm not sure where it comes from. Could that be the issue?
final item = ItemUtils.getCurrentItem(item, now);
be valid?item
is already defined, isn't it?x!
somewhere whenx
isnull
. Exactly what line is referenced from the stack trace?