avoid-watch-outside-build
- Group
- Correctness
- Path
lint/correctness/avoid-watch-outside-build- Default
- On (recommended)
- Origin
- Falcon
- Domain
- provider
- Rule
- 186 of 512
Flags listening Provider accesses proven to run outside Flutter builds.
Provider watch/select and listening Provider.of calls subscribe to widget
updates. Use read in initState, dispose, non-widget classes and deferred
callbacks. didChangeDependencies may establish dependencies; buildWithChild
on nested's SingleChildStatelessWidget/SingleChildState is a build entry.
Only confirmed Flutter BuildContext receivers with provider imports match.
Widget helpers, unresolved bases and callback arguments with an unknown
execution phase are skipped. Named on… callbacks are deferred events.
Invalid
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
void action(BuildContext context) {
context.watch<int>();
context.select<String, int>((value) => value.length);
Provider.of<String>(context);
Provider.of<int>(context, listen: true);
}
class View extends StatelessWidget {
Widget build(BuildContext context) {
final callback = () => context.watch<int>();
void local() { context.watch<String>(); }
return SizedBox();
}
}
class PlainService {
void action(BuildContext context) {
context.watch<int>();
}
void build(BuildContext context) {
Provider.of<int>(context);
}
}
class ViewState extends State<StatefulWidget> {
BuildContext get context => throw 0;
void initState() {
context.watch<int>();
}
void dispose() {
Provider.of<int>(context);
}
Widget build(BuildContext context) => GestureDetector(
onTap: () {
context.watch<int>();
},
);
}void action(BuildContext context) { context.watch<int>(); context.watch<int>(); context.select<String, int>((value) => value.length); context.select<String, int>((value) => value.length); Provider.of<String>(context); Provider.of<String>(context); Provider.of<int>(context, listen: true); Widget build(BuildContext context) { final callback = () => context.watch<int>(); final callback = () => context.watch<int>(); void local() { context.watch<String>(); } void action(BuildContext context) { context.watch<int>(); void build(BuildContext context) { Provider.of<int>(context); void initState() { context.watch<int>(); void dispose() { Provider.of<int>(context); onTap: () { context.watch<int>();Valid
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
class View extends StatelessWidget {
Widget build(BuildContext context) {
context.watch<int>();
context.select<String, int>((value) => value.length);
Provider.of<int>(context);
Provider.of<int>(context, listen: true);
return SizedBox();
}
// Helpers can establish dependencies while called synchronously from build.
Widget label(BuildContext context) => Text('${context.watch<int>()}');
void action(BuildContext context) {
context.watch<int>();
}
}
void callback(BuildContext context, bool listen) {
context.read<int>();
Provider.of<int>(context, listen: false);
Provider.of<int>(context, listen: listen);
}
class Other { T watch<T>() => throw 0; }
void unrelated(Other context) { context.watch<int>(); }Configure avoid-watch-outside-build
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-watch-outside-build": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-watch-outside-build: <reason>More correctness rules
- avoid-web-libraries-in-flutter
avoid_web_libraries_in_flutterrecommendedflutterDisallow importing web-only
dart:libraries from Flutter code. - await-only-futures
await_only_futuresrecommendedAwait only values assignable to futures;
FutureOr, dynamic, bottom types and extension types are accepted like the analyzer. - banned-importscross-file
Report imports forbidden by package-relative architectural boundaries.
- cancel-subscriptions
cancel_subscriptionsrecommendedCancel instances of
dart:asyncStreamSubscription. - check-is-not-closed-after-async-gaprecommendedbloc
Flags Bloc
addandemitcalls after an await without a freshisClosedcheck. - close-sinks
close_sinksrecommendedClose instances of
dart:coreSink. - collection-methods-unrelated-type
collection_methods_unrelated_typerecommendedReports collection lookups whose argument type is unrelated to the collection's type argument.
- comment-references
comment_referencesrecommendedFlags doc-comment references
[name]that resolve to nothing in scope.