I updated my flutter project to nullsafety and get errors saying:
The function can't be unconditionally invoked because it can be 'null'. Try adding a null check ('!').
For a variable that I already checked for null. What is the correct way to do this now? I know I can just add !
because i know it can't be null. But I don't see why. If there was more code it could happen, that the null check is deleted and the !
operator stays.
Here is my code example:
final Function? suggestionsCallback;
if (widget.suggestionsCallback != null &&
widget.defaultSearchPattern.isNotEmpty &&
pattern.isEmpty) {
return await widget.suggestionsCallback(widget.defaultSearchPattern); // here is the error
} else {
return widget.suggestionsCallback != null
? await widget.suggestionsCallback(pattern) // here is the error
: [];
}