Skip to main content

prefer-match-file-name

effort: 2m
configurable
has auto-fix
free+

Warns if the file name does not match the name of the first public class / mixin / extension / enum in the file or a private one if there are no public entries.

By default this rule skips .g.dart, .mapper.dart, and .freezed.dart files. To exclude other files with dot notation, use the exclude configuration.

note

For this rule it's recommended to exclude the test folder.

⚙️ Config

Set ignore-typedefs (default is true) to exclude typedef declarations from the name check.

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-match-file-name:
ignore-typedefs: true

Example

Example with 1 Class

❌ Bad:

some_widget.dart
// LINT: The first class name does not match the file name. Try renaming it.
class SomeOtherWidget extends StatelessWidget {

Widget build(BuildContext context) {
// ...
}
}

✅ Good:

some_widget.dart
class SomeWidget extends StatelessWidget {

Widget build(BuildContext context) {
// ...
}
}

Example with Several Classes

❌ Bad:

some_other_widget.dart
class _SomeOtherWidget extends StatelessWidget {

Widget build(BuildContext context) {
// ...
}
}

// LINT: The first class name does not match the file name. Try renaming it.
class SomeWidget extends StatelessWidget {

Widget build(BuildContext context) {
// ...
}
}

✅ Good:

some_widget.dart
class _SomeOtherWidget extends StatelessWidget {

Widget build(BuildContext context) {
// ...
}
}

// Correct, the file has a new name
class SomeWidget extends StatelessWidget {

Widget build(BuildContext context) {
// ...
}
}