positional_field_in_object_pattern
Object patterns can only use named fields.
Description
#The analyzer produces this diagnostic when an object pattern contains a field without specifying the getter name. Object pattern fields match against values that the object's getters return. Without a getter name specified, the pattern field can't access a value to attempt to match against.
Example
#The following code produces this diagnostic because the object pattern
String(1)
doesn't specify which getter of String
to access and compare
with the value 1
:
dart
void f(Object o) {
if (o case String(1)) {}
}
Common fixes
#Add the getter name to access the value, followed by a colon before the pattern to match against:
dart
void f(Object o) {
if (o case String(length: 1)) {}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。