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
:
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:
void f(Object o) {
if (o case String(length: 1)) {}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.