avoid-read-inside-build
- Group
- Correctness
- Path
lint/correctness/avoid-read-inside-build- Default
- On (recommended)
- Origin
- Falcon
- Domain
- provider
- Rule
- 117 of 512
Flags non-listening Provider reads directly inside Flutter widget builds.
A read does not rebuild the widget when its value changes. This checks
package:provider context extensions and Provider.of(listen: false) on a
Flutter BuildContext. Nested callbacks are skipped: their execution phase
depends on the receiving API, not their lexical position. BuildWithChild
overrides on nested's single-child widgets are also build entry points;
didChangeDependencies permits subscriptions but is not a build.
Invalid
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import 'package:nested/nested.dart';
class View extends StatelessWidget {
Widget build(BuildContext context) {
context.read<int>();
final value = (context.read<String>());
if (true) context.read<bool>();
Provider.of<int>(context, listen: false);
return context.read<Widget>();
}
}
class StatefulView extends StatefulWidget {
State<StatefulView> createState() => ViewState();
}
class ViewState extends State<StatefulView> {
Widget build(BuildContext other) {
return other.read<Widget>();
}
}
class SingleView extends SingleChildStatelessWidget {
Widget buildWithChild(BuildContext context, Widget? child) {
context.read<int>();
Provider.of<int>(context, listen: false);
return child ?? SizedBox();
}
}
class SingleState extends SingleChildState<SingleChildStatefulWidget> {
Widget buildWithChild(BuildContext context, Widget? child) {
context.read<int>();
Provider.of<int>(context, listen: false);
return child ?? SizedBox();
}
} Widget build(BuildContext context) { context.read<int>(); context.read<int>(); final value = (context.read<String>()); final value = (context.read<String>()); if (true) context.read<bool>(); if (true) context.read<bool>(); Provider.of<int>(context, listen: false); Provider.of<int>(context, listen: false); return context.read<Widget>(); Widget build(BuildContext other) { return other.read<Widget>(); Widget buildWithChild(BuildContext context, Widget? child) { context.read<int>(); context.read<int>(); Provider.of<int>(context, listen: false); Widget buildWithChild(BuildContext context, Widget? child) { context.read<int>(); context.read<int>(); Provider.of<int>(context, listen: false);Valid
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
class View extends StatelessWidget {
Widget build(BuildContext context) {
context.watch<int>();
Provider.of<int>(context);
Provider.of<int>(context, listen: true);
final callback = () => context.read<int>();
void local() { context.read<int>(); }
return SizedBox();
}
void callback(BuildContext context) { context.read<int>(); }
}
class NotAWidget {
void build(BuildContext context) { context.read<int>(); }
}
class Other { T read<T>() => throw 0; }
void readOther(Other context) { context.read<int>(); }
class DependentState extends State<StatefulWidget> {
BuildContext get context => throw 0;
void didChangeDependencies() {
context.read<int>();
Provider.of<int>(context, listen: false);
}
Widget helper(BuildContext context) => Text('${context.read<int>()}');
Widget build(BuildContext context) => helper(context);
}Configure avoid-read-inside-build
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-read-inside-build": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-read-inside-build: <reason>More correctness rules
- avoid-recursive-calls
Reports direct self-calls in functions and methods, including guarded recursion.
- avoid-recursive-widget-callsrecommendedflutter
Warns when a widget's build method constructs the same widget again.
- avoid-ref-inside-state-disposeriverpod
Rejects Riverpod widget references used during State.dispose.
- avoid-ref-read-inside-buildrecommendedriverpod
Flags one-time Riverpod reads of provider state during widget build.
- avoid-ref-watch-outside-buildrecommendedriverpod
Flags Riverpod WidgetRef.watch subscriptions outside synchronous widget build bodies.
- avoid-relative-lib-imports
avoid_relative_lib_importsrecommendedDisallow relative imports that reach into a
lib/directory. - avoid-returning-widgets
avoid-returning-widgetsflutterDisallow functions and methods that return a
Widget. - avoid-type-to-string
avoid_type_to_stringrecommendedAvoid
Type.toString()in production code, where type names may be minified.