相关疑难解决方法(0)

迁移到 Dart 空安全后,“无法无条件调用运算符,因为接收器可以为空”错误

我正在升级一个基于 Flutter 框架的个人包。我在 Flutter Text 小部件源代码中注意到这里有一个空检查:

if (textSpan != null) {
  properties.add(textSpan!.toDiagnosticsNode(name: 'textSpan', style: DiagnosticsTreeStyle.transition));
}
Run Code Online (Sandbox Code Playgroud)

但是,textSpan!仍然使用!运算符。不textSpan应该在不必使用!运算符的情况下提升为不可为空的类型吗?但是,尝试删除运算符会出现以下错误:

An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.
Run Code Online (Sandbox Code Playgroud)

这是一个独立的示例:

An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.
Run Code Online (Sandbox Code Playgroud)

我收到一个编译时错误:

错误:“字符串?”类型的值 …

dart type-promotion flutter dart-null-safety

18
推荐指数
2
解决办法
1万
查看次数

标签 统计

dart ×1

dart-null-safety ×1

flutter ×1

type-promotion ×1