What are the ?? double question marks in Dart?
Asked Answered
S

5

199

The following line of code has two question marks:

final myStringList = prefs.getStringList('my_string_list_key') ?? [];

What is the meaning?

Sizemore answered 4/1, 2019 at 0:48 Comment(0)
S
387

The ?? double question mark operator means "if null". Take the following expression, for example.

String a = b ?? 'hello';

This means a equals b, but if b is null then a equals 'hello'.

Another related operator is ??=. For example:

b ??= 'hello';

This means if b is null then set it equal to hello. Otherwise, don't change it.

Reference

Terms

The Dart 1.12 release news collectively referred to the following as null-aware operators:

  • ?? -- if null operator
  • ??= -- null-aware assignment
  • x?.p -- null-aware access
  • x?.m() -- null-aware method invocation
Sizemore answered 4/1, 2019 at 0:48 Comment(0)
S
16

(??) null operator, it returns the expression on its left when the it's not null, otherwise it'll return the right expression.

(??=) Null-aware assignment - This operator assigns value to the variable on its left, only if that variable is currently null.

(?.) Null-Aware access This operator prevents you from crashing your app by trying to access a property or a method of an object that might be null. For example,

String x;
print(x.toUpperCase());   // WILL GIVE AN ERROR
print(x?.toUpperCase()); // OUTPUT WILL BE NULL

(...?) Null-Aware spread operator - This operator prevents you from adding null elements using spread operator.

Scaramouch answered 3/8, 2022 at 12:57 Comment(0)
M
14

Dart offers some handy operators for dealing with values that might be null. One is the ??= assignment operator, which assigns a value to a variable only if that variable is currently null:

int a; // The initial value of a is null.
a ??= 3;
print(a); // <-- Prints 3.

a ??= 5;
print(a); // <-- Still prints 3.

Another null-aware operator is ??, which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right:

print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
Makhachkala answered 19/12, 2020 at 2:51 Comment(0)
P
3

?? is a null-aware operator, the expression means that, if the value of "my_string_list_key" in the prefs instance is null then assign an empty list to myStringList.

Placia answered 14/6, 2022 at 9:52 Comment(0)
C
2

This is especially useful in the copyWith method which is used in flutter very often to override. Here is an example below:

import './color.dart';
import './colors.dart';

class CoreState {
  final int counter;
  final Color backgroundColor;

  const CoreState({
    this.counter = 0,
    this.backgroundColor = Colors.white,
  });

  CoreState copyWith({
    int? counter,
    Color? backgroundColor,
  }) =>
      CoreState(
        counter: counter ?? this.counter,
        backgroundColor: backgroundColor ?? this.backgroundColor,
      );

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is CoreState &&
              runtimeType == other.runtimeType &&
              counter == other.counter &&
              backgroundColor == other.backgroundColor;

  @override
  int get hashCode => counter.hashCode ^ backgroundColor.hashCode;


  @override
  String toString() {
    return "counter: $counter\n"
            "color:$backgroundColor";
  }
}
Compose answered 26/7, 2021 at 9:30 Comment(1)
What we do in here is we give user chance to override, notice the nullable parameters in the copywith method and then the check whether the parameter is null default back to the existing value definedCompose

© 2022 - 2024 — McMap. All rights reserved.