Skip to main content

use-closest-build-context

Warns when the referenced BuildContext variable is not the closest BuildContext.

Using incorrect BuildContext reference can lead to hard to spot bugs and can happen if the variable was renamed from the usual context name (e.g. to _ due to being previously unused).

Example

❌ Bad:

class MyOtherWidget extends StatelessWidget {

Widget build(BuildContext context) {
return Builder(builder: (_) {
// LINT: This 'BuildContext' variable is not the closest context variable.
// Try using the closest context instead.
return _buildMyWidget(context);
});
}
}

✅ Good:

class MyOtherWidget extends StatelessWidget {

Widget build(BuildContext context) {
return Builder(builder: (context) { // correct, was renamed from '_' to 'context'
return _buildMyWidget(context);
});
}
}