nilts 0.9.0
nilts: ^0.9.0 copied to clipboard
nilts is lint rules, quick fixes and assists for Dart and Flutter projects that helps you enforce best practices, and avoid errors.
nilts #
nilts is lint rules, quick fixes and assists for Dart and Flutter projects that helps you enforce best practices, and avoid errors.
Contents #
- Usage
- Configuration
- Lint rules and quick fixes
- Assists
- Known issues
- Feature requests
- Bug reports
- Contributing
Usage #
nilts depends on custom_lint
.
You should add nilts
and custom_lint
to your dev_dependencies
in pubspec.yaml
file.
dev_dependencies:
custom_lint: <version>
nilts: <version>
And also, add custom_lint
to your analysis_options.yaml
file.
analyzer:
plugins:
- custom_lint
Configuration #
You can configure all lint rules provided by nilts
in analysis_options.yaml
file.
Choice one of the following configuration strategies.
Disabling strategy #
All of nilts
rules are enabled by default.
Add lint rule name and set false
to disable it.
custom_lint:
rules:
# Disable particular lint rules if you want ignore them whole package.
- unnecessary_rebuilds_from_media_query: false
Enabling strategy #
You can disable all lint rules depends on custom_lint by setting enable_all_lint_rules
to false
.
Add lint rule name and set true
to enable it.
custom_lint:
# Disable all lint rules depends on custom_lint.
enable_all_lint_rules: false
rules:
- unnecessary_rebuilds_from_media_query: true
NOTE: If you enable_all_lint_rules
set to false
, all of lint rules (not only all of nilts's lint rules) depends on custom_lint
will be disabled by default.
Lint rules and quick fixes #
Read below to learn about each lint rules intend to.
Some of lint rules support quick fixes on IDE.
Overview #
Rule name | Overview | Target SDK | Rule type | Maturity level | Quick fix |
---|---|---|---|---|---|
defined_void_callback_type | Checks void Function() definitions. |
Any versions nilts supports | Practice | Experimental | ✅️ |
fixed_text_scale_rich_text | Checks usage of textScaler or textScaleFactor in RichText constructor. |
Any versions nilts supports | Practice | Experimental | ✅️ |
flaky_tests_with_set_up_all | Checks setUpAll usages. |
Any versions nilts supports | Practice | Experimental | ✅️ |
no_support_multi_text_direction | Checks if supports TextDirection changes |
Any versions nilts supports | Practice | Experimental | ✅️ |
shrink_wrapped_scroll_view | Checks the content of the scroll view is shrink wrapped. | Any versions nilts supports | Practice | Experimental | ✅️ |
unnecessary_rebuilds_from_media_query | Checks MediaQuery.xxxOf(context) or MediaQuery.maybeXxxOf(context) usages. |
>= Flutter 3.10.0 (Dart 3.0.0) | Practice | Experimental | ✅️ |
Details #
defined_void_callback_type
- Target SDK: Any versions nilts supports
- Rule type: Practice
- Maturity level: Experimental
- Quick fix: ✅
Consider replace void Function()
with VoidCallback
which is defined in Flutter SDK.
BAD:
final void Function() callback;
GOOD:
final VoidCallback callback;
fixed_text_scale_rich_text
- Target SDK: Any versions nilts supports
- Rule type: Practice
- Maturity level: Experimental
- Quick fix: ✅
Consider using Text.rich
or adding textScaler
or textScaleFactor
(deprecated on Flutter 3.16.0 and above) argument to [RichText] constructor to make the text size responsive for user setting.
BAD:
RichText(
text: TextSpan(
text: 'Hello, world!',
),
)
GOOD:
Text.rich(
TextSpan(
text: 'Hello, world!',
),
)
GOOD:
RichText(
text: TextSpan(
text: 'Hello, world!',
),
textScaler: MediaQuery.textScalerOf(context),
)
GOOD (deprecated on Flutter 3.16.0 and above):
RichText(
text: TextSpan(
text: 'Hello, world!',
),
textScaleFactor: MediaQuery.textScaleFactorOf(context),
)
See also:
- Text.rich constructor - Text - widgets library - Dart API
- RichText class - widgets library - Dart API
flaky_tests_with_set_up_all
- Target SDK: Any versions nilts supports
- Rule type: Practice
- Maturity level: Experimental
- Quick fix: ✅
Consider using setUp
function or initialization on top level or body of test group.
setUpAll
may cause flaky tests with concurrency executions.
BAD:
setUpAll(() {
// ...
});
GOOD:
setUp(() {
// ...
});
void main() {
// do initialization on top level
// ...
group('...', () {
// or do initialization on body of test group
// ...
});
}
See also:
- setUpAll function - flutter_test library - Dart API
- setUp function - flutter_test library - Dart API
no_support_multi_text_direction
- Target SDK: Any versions nilts supports
- Rule type: Practice
- Maturity level: Experimental
- Quick fix: ✅
Consider using TextDirection
aware configurations if your application supports different TextDirection
languages.
BAD:
Align(
alignment: Alignment.bottomLeft,
)
BAD:
Padding(
padding: EdgeInsets.only(left: 16, right: 4),
)
BAD:
Positioned(left: 12, child: SizedBox())
GOOD:
Align(
alignment: AlignmentDirectional.bottomStart,
)
GOOD:
Padding(
padding: EdgeInsetsDirectional.only(start: 16, end: 4),
)
GOOD:
Positioned.directional(
start: 12,
textDirection: TextDirection.ltr,
child: SizedBox(),
)
PositionedDirectional(
start: 12,
child: SizedBox(),
)
See also:
- TextDirection enum - dart:ui library - Dart API
- AlignmentDirectional class - painting library - Dart API
- EdgeInsetsDirectional class - painting library - Dart API
- PositionedDirectional class - widgets library - Dart API
shrink_wrapped_scroll_view
- Target SDK: Any versions nilts supports
- Rule type: Practice
- Maturity level: Experimental
- Quick fix: ✅
Consider removing shrinkWrap
argument and update the Widget not to shrink wrap.
Shrink wrapping the content of the scroll view is significantly more expensive than expanding to the maximum allowed size because the content can expand and contract during scrolling, which means the size of the scroll view needs to be recomputed whenever the scroll position changes.
You can avoid shrink wrap with 3 steps below in case of your scroll view is nested.
- Replace the parent scroll view with
CustomScrollView
. - Replace the child scroll view with
SliverListView
orSliverGridView
. - Set
SliverChildBuilderDelegate
todelegate
argument of theSliverListView
orSliverGridView
.
BAD:
ListView(shrinkWrap: true)
GOOD:
ListView(shrinkWrap: false)
See also:
- shrinkWrap property - ScrollView class - widgets library - Dart API
- ShrinkWrap vs Slivers | Decoding Flutter - YouTube
unnecessary_rebuilds_from_media_query
- Target SDK: >= Flutter 3.10.0 (Dart 3.0.0)
- Rule type: Practice
- Maturity level: Experimental
- Quick fix: ✅
Prefer using MediaQuery.xxxOf
or MediaQuery.maybeXxxOf
instead of MediaQuery.of
or MediaQuery.maybeOf
to avoid unnecessary rebuilds.
BAD:
final size = MediaQuery.of(context).size;
GOOD:
final size = MediaQuery.sizeOf(context);
Note that using MediaQuery.of
or MediaQuery.maybeOf
makes sense following cases:
- wrap Widget with
MediaQuery
overriddenMediaQueryData
- observe all changes of
MediaQueryData
See also:
- MediaQuery as InheritedModel by moffatman · Pull Request #114459 · flutter/flutter
- MediaQuery class - widgets library - Dart API
Assists #
Upcoming... 🚀
Known issues #
Quick fix priorities #
Priorities of quick fixes are not reflected on IntelliJ IDEA and Android Studio.
VS Code is supported to list with these priorities.
VS Code | IntelliJ IDEA / Android Studio |
---|---|
See also:
Feature requests #
If you have any feature requests, please create an issue from this template.
Bug reports #
If you find any bugs, please create an issue from this template.
Contributing #
Welcome your contributions!!
Please read CONTRIBUTING docs before submitting your PR.