sangria_lints 0.0.3 copy "sangria_lints: ^0.0.3" to clipboard
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:
copied to clipboard

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 your analysis_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
copied to clipboard

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),
      );
    }
  }
copied to clipboard

✅ 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),
    );
  }
}
copied to clipboard
0
likes
160
points
188
downloads

Publisher

verified publisheroffich.me

Weekly Downloads

2024.09.21 - 2025.04.05

Lint Rules for personal developments created by custom_lint_builder package.

Homepage
Repository (GitHub)
View/report issues

Topics

#lints #analysis #code-style

Documentation

API reference

License

MIT (license)

Dependencies

analyzer, analyzer_plugin, custom_lint_builder, flutter

More

Packages that depend on sangria_lints