Skip to main content

use_nearest_context

A rule which checks that we use BuildContext from the nearest available scope.

Example:

BAD:

class SomeWidget extends StatefulWidget {
...
}

class _SomeWidgetState extends State<SomeWidget> {
...
void _showDialog() {
showModalBottomSheet(
context: context,
builder: (BuildContext _) {
final someProvider = context.watch<SomeProvider>(); // LINT, BuildContext is used not from the nearest available scope

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

GOOD:

class SomeWidget extends StatefulWidget {
...
}

class _SomeWidgetState extends State<SomeWidget> {
...
void _showDialog() {
showModalBottomSheet(
context: context,
builder: (BuildContext context)
final someProvider = context.watch<SomeProvider>(); // OK

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