avoid-read-inside-build
Warns when a read
method is used inside the build
method.
read
is designed to read the value one time which is, if done inside the build
method, can lead to missing the event when the provided value changes.
Example
❌ Bad:
class MyHomePage extends StatelessWidget {
const MyHomePage();
Widget build(BuildContext context) {
// LINT: Avoid using 'read' inside the 'build' method. Try rewriting the code to use 'watch' instead.
final value = context.read<int>();
return Scaffold(
...
);
}
}
✅ Good:
class MyHomePage extends StatelessWidget {
const MyHomePage();
Widget build(BuildContext context) {
final value = context.watch<int>();
return Scaffold(
...
);
}
}