🎯 Operators

Review common operators in Dart

Let’s take a look at some of the more interesting operators in Dart.

Assignment

Use the assignment operator to assign a value to a variable, ONLY if it’s not already assigned.

file_type_dartlang main.dart
String? name;
name ??= 'Guest';
var z = name ?? 'Guest';

Ternary Operator

Use a ternary to replace the if/else statement.

file_type_dartlang main.dart
String color = 'blue';
var isThisBlue = color == 'blue' ? 'Yep, blue it is' : 'Nah, it aint blue';

Cascade

Use the cascade operator to call methods on an object without having to create a new line of code. It can often eliminate the need to create a temporary variable, which is especially useful in with working in Flutter’s widget tree.

file_type_dartlang main.dart
// var paint = Paint();
// paint.color = 'black';
// paint.strokeCap = 'round';
// paint.strokeWidth = 5.0;

var paint = Paint()
    ..color = 'black'
    ..strokeCap = 'round'
    ..strokeWidth = 5.0;

Typecast

In rare cases, you may need to cast a value to a different type.

file_type_dartlang main.dart
var number = 23 as String;
number is String; // true

Questions? Let's chat

Open Discord