use-existing-widget
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
sequenceto configure the sequence of widgets (separated by>) that should be replaced. To provide a specific argument name, addname:before the widget name (useschild:if omitted). To highligh usage in any named parameter, passany: - Set
replacementto 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());