什么是 ??Dart中的双问号? [英] What are the ?? double question marks in Dart?

查看:63
本文介绍了什么是 ??Dart中的双问号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面这行代码有两个问号:

The following line of code has two question marks:

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

什么意思?

推荐答案

?? 双问号运算符的意思是if null".以下面的表达式为例.

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

String a = b ?? 'hello';

这意味着 a 等于 b,但如果 b 为空,则 a 等于 'hello'.

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

另一个相关的运算符是 ??=.例如:

Another related operator is ??=. For example:

b ??= 'hello';

这意味着如果 b 为空,则将其设置为等于 hello.否则,不要改变它.

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

参考

条款

Dart 1.12 发布新闻 以下统称为空感知运算符:

  • ?? -- 如果为空操作符
  • ??= -- 空感知赋值
  • x?.p -- 空感知访问
  • x?.m() -- 空感知方法调用
  • ?? -- if null operator
  • ??= -- null-aware assignment
  • x?.p -- null-aware access
  • x?.m() -- null-aware method invocation

这篇关于什么是 ??Dart中的双问号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆