什么是 ?? Dart中有两个问号? [英] What are the ?? double question marks in Dart?

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

问题描述

下面的代码行带有两个问号:

The following line of code has two question marks:

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

什么意思?

推荐答案

??双问号运算符的意思是如果为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.

参考

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

条款

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天全站免登陆