Pau*_*Cbt 0 dart flutter dart-null-safety
两者之间到底有什么区别
String id = folderInfo!.first.id; //this works
Run Code Online (Sandbox Code Playgroud)
和
String id = folderInfo?.first.id; //this is an error
Run Code Online (Sandbox Code Playgroud)
我知道 ?。当值对象为 null 时返回 null,但是 !. 返回?
?.称为有条件成员访问
\n\n最左边的操作数可以为空;示例: foo?.bar 从表达式 foo 中选择属性 bar,除非 foo 为 null(在这种情况下,foo?.bar 的值为 null)
\n
就您而言,String id手段id没有null价值。但是使用?.可以返回null,这就是它显示错误的原因。
!.如果您知道表达式永远不会计算为 ,则使用.null
例如int类型的变量?可能是整数,也可能为空。如果您知道表达式永远不会计算为 null,但 Dart 不同意,您可以添加!断言它不是 \xe2\x80\x99t null(如果是,则抛出异常)。
| 归档时间: |
|
| 查看次数: |
1260 次 |
| 最近记录: |