Search code snippets, questions, articles...

Assigning a value if it's null

Assigning a new value to a variable if it's null
void main() {
  int? a; // a is null here
  a ??= 10; // will assign 10 to a as its null
  print(a); // -> Prints 10.

  a ??= 50; // will not assign 50
  print(a); // <-- Still Prints 10.

  var name = null ?? 'Ankit'; //Will assign Ankit as first value is null

Search Index Data (The code snippet can also be found with below search text)

Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet