[英]What are the ?? double question marks in Dart?
The following line of code has two question marks:以下代码行有两个问号:
final myStringList = prefs.getStringList('my_string_list_key') ?? [];
What is the meaning?什么意思?
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'
.这意味着a
等于b
,但如果b
为空,则a
等于'hello'
。
Another related operator is ??=
.另一个相关的运算符是??=
。 For example:例如:
b ??= 'hello';
This means if b
is null then set it equal to hello
.这意味着如果b
为空,则将其设置为等于hello
。 Otherwise, don't change it.否则,不要改变它。
Reference参考
Terms条款
The Dart 1.12 release news collectively referred to the following as null-aware operators : Dart 1.12 发布消息将以下内容统称为null-aware operator :
??
-- if null operator -- 如果为空运算符??=
-- null-aware assignment ??=
-- 空感知赋值x?.p
-- null-aware access x?.p
-- 空感知访问x?.m()
-- null-aware method invocation x?.m()
-- 空感知方法调用Dart offers some handy operators for dealing with values that might be null. Dart 提供了一些方便的运算符来处理可能为 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: ,它返回其左侧的表达式,除非该表达式的值为 null,在这种情况下,它计算并返回其右侧的表达式:
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
This is especially useful in the copyWith method which is used in flutter very often to override.这在颤振中经常用于覆盖的 copyWith 方法中特别有用。 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";
}
}
??
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
.是一个 null 感知运算符,该表达式意味着,如果prefs
实例中"my_string_list_key"
的值为null
,则将一个空列表分配给myStringList
。
(??) null operator , it returns the expression on its left when the it's not null
, otherwise it'll return the right expression. (??) null 运算符,如果left when the it's not null
,则返回左侧表达式,否则返回右侧表达式。
(??=) Null-aware assignment - This operator assigns value to the variable on its left, only if that variable is currently null. (??=) Null-aware assignment - 此运算符将值分配给其左侧的变量,仅当该变量当前为 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. (?.) Null-Aware access此操作符通过尝试访问可能是 null 的 object 的属性或方法来防止您的应用程序崩溃。 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. (...?) Null-Aware spread operator - 此运算符阻止您使用扩展运算符添加 null 元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.