Is there "!." operator in dart (flutter)?

Viewed 2854

Flutter source files contains many times code similar to this:

 @override
  double computeMinIntrinsicWidth(double height) {
    if (child != null)
      return child!.getMinIntrinsicWidth(height);
    return 0.0;
  }

Please explain "!." I can't find it on list of dart operators.

2 Answers

A postfix exclamation mark (!) takes the expression on the left and casts it to its underlying non-nullable type. So it changes:

String toString() {
  if (code == 200) return 'OK';
  return 'ERROR $code ${(error as String).toUpperCase()}';
}

to something like this:

String toString() {
  if (code == 200) return 'OK';
  return 'ERROR $code ${error!.toUpperCase()}';
}

You can read more about null safety in this document.

Related