Option A: not working
var a = <Widget?>[Container(), null];
a.removeNulls();
print(a.length == 1 && a[0] != null) // true
var b = a as List<Widget>; // _CastError (type 'List<Widget?>' is not a subtype of type 'List<Widget>' in type cast)
Option B: working
var a = <Widget?>[Container(), null];
a = a.whereType<Widget>().toList();
var b = a as List<Widget>; // works fine
.removeNull() is part of fast_immutable_collections
I'm not familiar with the package, but quickly looking up the extension method shows that's just a wrapper over another function,
removeWhere, which has nothing to do with nullability.Try
print(a.runtimeType)and you'll seeList<Widget?>. Thus, error thrown is correct.As to why it is the case with the library, only the owner can answer that.