Piotr Temp
Piotr Temp

Reputation: 435

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

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.

Upvotes: 3

Views: 4950

Answers (2)

Payam Asefi
Payam Asefi

Reputation: 2757

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.

Upvotes: 3

A.A
A.A

Reputation: 4101

It's the "(not)null assertion operator" which becomes part of Dart with the Null Safety feature in the next release.

Upvotes: 0

Related Questions