Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

[New rule] use-setstate-synchronously #1117

Closed
@Desdaemon

Description

@Desdaemon

Please describe what the rule should do:

(Flutter-specific) Attempts to call setState after the state has been disposed usually represents a potential bug. To remedy this, the user needs to either:

  1. Insert a check for mounted and call setState in it
  2. Insert a guard for !mounted that diverges

Ideally, this rule should also be customizable to detect/forbid usages other than setState.

If your rule is inspired by other please provide link to it:

What category of rule is this? (place an "X" next to just one item)

  • Warns about a potential error (problem)
  • Suggests an alternate way of doing something (suggestion)
  • Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

In methods:

class FooState extends State {
  fetchData() async {
    final data = await ...;
    setState(() { ... });
  }
}

In closures:

Widget build(context) {
  return FooWidget(
    ..
    onChange: (value) async {
      await fetchData(...);
      setState(() { ... });
    }
  );
}

Conditional await (would mounted checking be mandatory in this case?)

void fetchCacheableData() async {
  final data = someCondition ? fromCache : await ...;
  setState(() { ... });
}

Are you willing to submit a pull request to implement this rule?
Yes

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions