Question mark before dot in Dart

大兔子大兔子 提交于 2020-01-30 11:22:28

问题


What's the meaning of the question mark in this piece of code? And when am I supposed to use it? My code functions the same way with or without the question mark.

void dispose(){
  bloc?.dispose();
  super.dispose();
}

回答1:


The question mark is one of the null-aware operators in Dart. In this example it says: call the dispose() method only if bloc is not null. Without the question mark, if bloc was null when it tried to call dispose() a NoSuchMethodError would be thrown.

There is a useful section on the Dart site about null-aware operators:

https://dart.dev/codelabs/dart-cheatsheet



来源:https://stackoverflow.com/questions/56750970/question-mark-before-dot-in-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!