Skip to main content

use-existing-widget

effort: 4m
configurable
pro+

Warns when the sequence of widgets matches one of the configured sequences and should be replaced with another existing widget.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

Set entries (default is empty) to configure the list of entries for usages to ban.

Each entry is an object with 3 fields (sequence, replacement and severity):

  • Set sequence to configure the sequence of widgets (separated by >) that should be replaced. To provide a specific argument name, add name: before the widget name (uses child: if omitted). To highligh usage in any named parameter, pass any:
  • Set replacement to configure the name of the widget that should replace the sequence.
  • Set severity (optional) to override default severity for the given entry.
analysis_options.yaml
dcm:
rules:
- use-existing-widget:
entries:
- sequence: 'MyWidget > AnotherWidget > title: ThirdWidget'
replacement: 'SomeOtherWidget'
- sequence: 'MyWidget > any: AnotherWidget'
replacement: 'SomeWidget'
- sequence: 'MyWidget > OtherWidget'
replacement: 'SomeWidget'

Example

❌ Bad:

// LINT: This sequence of widgets (MyWidget > child: AnotherWidget > title: ThirdWidget) can be replaced with a single 'SomeOtherWidget' widget.
MyWidget(
child: AnotherWidget(title: ThirdWidget(another: Text())),
);

✅ Good:

SomeOtherWidget(child: Text());