What are the ?? double question marks in Dart?

后端 未结 1 1825
慢半拍i
慢半拍i 2020-11-29 01:08

The following line of code has two question marks:

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

What is the mean

相关标签:
1条回答
  • 2020-11-29 01:35

    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

    • A Tour of the Dart Language: Operators
    • Null-aware operators in Dart

    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
    0 讨论(0)
提交回复
热议问题