solid_lints icon indicating copy to clipboard operation
solid_lints copied to clipboard

Implement "use_nearest_context" rule

Open yurii-prykhodko-solid opened this issue 1 year ago • 0 comments

Right now we have an unused_parameters rule that forces us to rename all unused parameters to underscores.

In some cases, we not use context in widget builder functions initially, and add a usage later. There is some potential of using an incorrect instance of BuildContext that way.

We want to add a lint that checks that we use BuildContext from nearest available scope.

Example:

class SomeWidget extends StatefulWidget {
...
}

class _SomeWidgetState extends State<SomeWidget> {
  ...
  void _showDialog() {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext _) {                <-- LINT -- this should be renamed to `context`
        final someProvider = context.watch<SomeProvider>();   <-- uses context from StatefulWidget instead of the builder, which causes errors.

        return const SizedBox.shrink();
      },
    );
  }
}

We also probably can provide a quick fix here.

yurii-prykhodko-solid avatar Dec 19 '24 19:12 yurii-prykhodko-solid