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
  print(name);
}
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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

null
Was this helpful?
0 Comments