prefer-first
Same as prefer-first in Dart Code Metrics
- Group
- Style
- Path
lint/style/prefer-first- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Rule
- 358 of 512
Flags [0] index access that should use the .first getter.
Reading the leading element as xs.first states the intent directly and is
easier to read than the numeric xs[0], which forces the reader to recognize
that zero means "first". The rule matches an index expression whose subscript
is the integer literal 0; any other index, including a non-literal
expression that evaluates to zero, is left alone. Matching is syntactic on the
literal, so it does not confirm the receiver exposes a first getter.
Invalid
// Bad: using [0] instead of .first
void example() {
final items = [1, 2, 3];
final first = items[0];
print(first);
}
class Processor {
String getFirstName(List<String> names) {
return names[0];
}
void processHead(List<int> values) {
final head = values[0];
if (head > 0) {
compute(head);
}
}
}
void multipleViolations(List<String> items) {
final a = items[0];
final b = items[0].length;
print('$a $b');
}
// Regression: the violation must be found inside Dart 3 containers too.
void containers(List<int> xs, int count) {
final (a, _) = (xs[0], 0);
int b;
int _u;
(b, _u) = (xs[0], 0);
lbl: {
final c = xs[0];
print(c);
}
final d = switch (count) {
0 => xs[0],
_ => 0,
};
final e = switch (xs[0]) {
_ => 0,
};
final f = [if (count > 0) xs[0]];
final g = [...[xs[0]]];
final h = (p: xs[0], q: 0);
assert(xs[0] > 0, "x");
print([a, b, d, e, f, g, h]);
} final items = [1, 2, 3]; final first = items[0]; String getFirstName(List<String> names) { return names[0]; void processHead(List<int> values) { final head = values[0];void multipleViolations(List<String> items) { final a = items[0]; final a = items[0]; final b = items[0].length;void containers(List<int> xs, int count) { final (a, _) = (xs[0], 0); int _u; (b, _u) = (xs[0], 0); lbl: { final c = xs[0]; final d = switch (count) { 0 => xs[0], }; final e = switch (xs[0]) { }; final f = [if (count > 0) xs[0]]; final f = [if (count > 0) xs[0]]; final g = [...[xs[0]]]; final g = [...[xs[0]]]; final h = (p: xs[0], q: 0); final h = (p: xs[0], q: 0); assert(xs[0] > 0, "x");Valid
// Good: using .first
void example() {
final items = [1, 2, 3];
final first = items.first;
print(first);
}
class Processor {
String getFirstName(List<String> names) {
return names.first;
}
void processHead(List<int> values) {
final head = values.first;
if (head > 0) {
compute(head);
}
}
}
void multipleAccess(List<String> items) {
final a = items.first;
final b = items.first.length;
print('$a $b');
}
// OK: accessing non-zero indices
void accessOther(List<int> items) {
final second = items[1];
final third = items[2];
}
// OK: in loop context
void loopAccess(List<List<int>> matrix) {
for (int i = 0; i < matrix.length; i++) {
final row = matrix[i]; // accessing by variable index is OK
}
}Configure prefer-first
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-first": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-first: <reason>More style rules
- prefer-for-in
Flags zero-based, unit-step index loops over a syntactically known list.
- prefer-for-loop-in-childrenflutter
Prefers collection-for elements over eager map/toList chains in Flutter children.
- prefer-function-declarations-over-variables
prefer_function_declarations_over_variablesrecommendedFlags a local variable bound to a function literal that should be a local function declaration.
- prefer-generic-function-type-aliases
prefer_generic_function_type_aliasesrecommendedFlags old-style function typedefs written with inline parameter syntax.
- prefer-getter-over-method
Prefer getters for synchronous, zero-argument methods that only read a field.
- prefer-initializing-formals
prefer_initializing_formalsrecommendedPrefer initializing formals when a parameter is copied directly into its field.
- prefer-inlined-adds
prefer_inlined_addsrecommendedFlags an
addcascade on a list literal whose element could be written inline. - prefer-int-literals
prefer_int_literalsFlags a whole-number double literal, such as
1.0, where the context type isdoubleso the int literal1means the same value.