Skip to content

Implement "use_nearest_context" rule #190

@yurii-prykhodko-solid

Description

@yurii-prykhodko-solid

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions