avoid-watch-outside-build

Path
lint/correctness/avoid-watch-outside-build
Default
On (recommended)
Origin
Falcon
Domain
provider
Rule
186 of 512
recommendedprovider

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

example.dartdart
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>();
    },
  );
}
Use read instead of subscribing outside build.
4void action(BuildContext context) {
5 context.watch<int>();
Use read instead of subscribing outside build.
5 context.watch<int>();
6 context.select<String, int>((value) => value.length);
Use read instead of subscribing outside build.
6 context.select<String, int>((value) => value.length);
7 Provider.of<String>(context);
Use read instead of subscribing outside build.
7 Provider.of<String>(context);
8 Provider.of<int>(context, listen: true);
Use read instead of subscribing outside build.
11 Widget build(BuildContext context) {
12 final callback = () => context.watch<int>();
Use read instead of subscribing outside build.
12 final callback = () => context.watch<int>();
13 void local() { context.watch<String>(); }
Use read instead of subscribing outside build.
19 void action(BuildContext context) {
20 context.watch<int>();
Use read instead of subscribing outside build.
22 void build(BuildContext context) {
23 Provider.of<int>(context);
Use read instead of subscribing outside build.
29 void initState() {
30 context.watch<int>();
Use read instead of subscribing outside build.
32 void dispose() {
33 Provider.of<int>(context);
Use read instead of subscribing outside build.
36 onTap: () {
37 context.watch<int>();

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-watch-outside-build": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/avoid-watch-outside-build: <reason>
  • avoid-web-libraries-in-flutteravoid_web_libraries_in_flutterrecommendedflutter

    Disallow importing web-only dart: libraries from Flutter code.

  • await-only-futuresawait_only_futuresrecommended

    Await 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-subscriptionscancel_subscriptionsrecommended

    Cancel instances of dart:async StreamSubscription.

  • check-is-not-closed-after-async-gaprecommendedbloc

    Flags Bloc add and emit calls after an await without a fresh isClosed check.

  • close-sinksclose_sinksrecommended

    Close instances of dart:core Sink.

  • collection-methods-unrelated-typecollection_methods_unrelated_typerecommended

    Reports collection lookups whose argument type is unrelated to the collection's type argument.

  • comment-referencescomment_referencesrecommended

    Flags doc-comment references [name] that resolve to nothing in scope.