sangria_lints 0.0.3
sangria_lints: ^0.0.3 copied to clipboard
Lint Rules for personal developments created by custom_lint_builder package.
Sangria Lints #
Custom lints wanted for personal developments.
Table of content #
Getting started #
Add Sangria_lints #
Add sangria_lints to your pubspec.yaml
:
dev_dependencies:
sangria_lints:
Enable custom_lint #
sangria_lints comes bundled with its own rules using custom_lints.
-
Add both sangria_lints and custom_lint to your
pubspec.yaml
:dev_dependencies: sangria_lints: custom_lint: # <- add this
copied to clipboard -
Enable
custom_lint
's plugin in youranalysis_options.yaml
:analyzer: plugins: - sangria_lints
copied to clipboard
Disabling lint rules #
By default when installing sangria_lints, all the lints will be enabled. To change this, you have a few options.
analyzer:
plugins:
- custom_lint
custom_lint:
rules:
# Explicitly disable one custom-lint rule.
- use_setstate_synchronously: false
All custom-lint rules in sangria_lints #
use_setstate_synchronously #
A use_setstate_synchronously
rule that discourages the use of setState across asynchronous gaps within subclasses of State.
In async functions, the state of a widget may have been disposed across asynchronous gaps in a case when the user moves to a different screen. This leads to setState() called after dispose()
error.
Since widgets can be unmounted before a Future gets resolved, seeing if widgets are mounted is necessary before calling setState.
Example #
❌ BAD
class _MyWidgetState extends State<MyWidget> {
String message;
@override
Widget build(BuildContext context) {
return Button(
onPressed: () async {
String fromSharedPreference = await getFromSharedPreference();
// LINT: Avoid calling 'setState' across asynchronous gaps without seeing if the widget is mounted.
setState(() {
message = fromSharedPreference;
});
},
child: Text(message),
);
}
}
✅ GOOD
class _MyWidgetState extends State<MyWidget> {
String message;
@override
Widget build(BuildContext context) {
return Button(
onPressed: () async {
String fromSharedPreference = await getFromSharedPreference();
if (mounted) {
setState(() {
message = fromSharedPreference;
});
}
},
child: Text(message),
);
}
}