Whats ??= operator in Dart

427

Solution 1

??= is a new null-aware operators. Specifically ??= is null-aware assignment operator.

?? if null operator. expr1 ?? expr2 evaluates to expr1 if not null, otherwise expr2.

??= null-aware assignment. v ??= expr causes v to be assigned expr only if v is null.

?. null-aware access. x?.p evaluates to x.p if x is not null, otherwise evaluates to null.

Solution 2

?? is a null check operator.

String name=person.name ?? 'John';

if person.name is null, then name is assigned a value of “John”.

??= simply means “If left-hand side is null, carry out assignment”. This will only assign a value if the variable is null.

splashFactory ??= InkSplash.splashFactory;
Share:
427
Hashem Aboonajmi
Author by

Hashem Aboonajmi

Updated on December 25, 2022

Comments

  • Hashem Aboonajmi
    Hashem Aboonajmi over 1 year

    This is the new assignment operator I see in Flutter source code:

    splashFactory ??= InkSplash.splashFactory;
    textSelectionColor ??= isDark ? accentColor : primarySwatch[200];
    

    what's the meaning of this assignment operator?

    example in Flutter source code