prefer-ref-read-for-methods

Path
lint/correctness/prefer-ref-read-for-methods
Default
On (recommended)
Origin
Falcon
Domain
riverpod
Rule
397 of 512
recommendedriverpod

Flags watching a Riverpod notifier solely to invoke one of its methods.

Actions should read the notifier rather than establish a rebuild subscription. Only package-confirmed Ref receivers and notifier types are matched.

Invalid

example.dartdart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart' as hooks;
final countProvider = Provider<int>((ref) => 0);
class Counter extends Notifier<int> { void increment() {} void reset() {} int get value => 0; }
class AsyncCounter extends AsyncNotifier<int> { void refresh() {} }
final counterProvider = Provider<Counter>((ref) => Counter());
final asyncProvider = Provider<AsyncCounter>((ref) => AsyncCounter());
void actions(WidgetRef ref) {
  ref.watch(counterProvider).increment();
  ref.watch(counterProvider).reset();
  final increment = ref.watch(counterProvider).increment;
  ref.watch<Counter>(counterProvider).increment();
  (ref.watch(counterProvider)).reset();
  ref.watch(asyncProvider).refresh();
}
Use `ref.read` rather than `ref.watch` when invoking notifier methods.
8void actions(WidgetRef ref) {
9 ref.watch(counterProvider).increment();
Use `ref.read` rather than `ref.watch` when invoking notifier methods.
9 ref.watch(counterProvider).increment();
10 ref.watch(counterProvider).reset();
Use `ref.read` rather than `ref.watch` when invoking notifier methods.
10 ref.watch(counterProvider).reset();
11 final increment = ref.watch(counterProvider).increment;
Use `ref.read` rather than `ref.watch` when invoking notifier methods.
11 final increment = ref.watch(counterProvider).increment;
12 ref.watch<Counter>(counterProvider).increment();
Use `ref.read` rather than `ref.watch` when invoking notifier methods.
12 ref.watch<Counter>(counterProvider).increment();
13 (ref.watch(counterProvider)).reset();
Use `ref.read` rather than `ref.watch` when invoking notifier methods.
13 (ref.watch(counterProvider)).reset();
14 ref.watch(asyncProvider).refresh();

Valid

example.dartdart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart' as hooks;
final countProvider = Provider<int>((ref) => 0);
class Counter extends Notifier<int> { void increment() {} void reset() {} int get value => 0; }
class AsyncCounter extends AsyncNotifier<int> { void refresh() {} }
final counterProvider = Provider<Counter>((ref) => Counter());
final asyncProvider = Provider<AsyncCounter>((ref) => AsyncCounter());
void actions(WidgetRef ref) {
  ref.read(counterProvider).increment();
  final increment = ref.read(counterProvider).increment;
  final state = ref.watch(counterProvider).value;
  final notifier = ref.watch(counterProvider);
  notifier.increment();
  ref.watch(counterProvider).toString();
}
class Service { void invoke() {} }
final serviceProvider = Provider<Service>((ref) => Service());
void service(WidgetRef ref) { ref.watch(serviceProvider).invoke(); }
void unknown(dynamic ref) { ref.watch(counterProvider).increment(); }

Configure prefer-ref-read-for-methods

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "prefer-ref-read-for-methods": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/prefer-ref-read-for-methods: <reason>
  • prefer-relative-importsprefer_relative_imports

    Prefer relative imports for libraries of the same package under lib/.

  • prefer-sealed-bloc-eventsrecommendedbloc

    Flags Bloc event classes without a sealed or final modifier.

  • prefer-sealed-bloc-staterecommendedbloc

    Flags Bloc state classes without a sealed or final modifier.

  • prefer-void-to-nullprefer_void_to_nullrecommended

    Flags uses of the Null type where void is meant.

  • proper-controller-disposeproper_controller_disposerecommendedflutter

    Requires Flutter controllers owned by a State field to be disposed.

  • proper-expanded-and-flexibleproper_expanded_and_flexiblerecommendedflutter

    Require Expanded and Flexible to sit directly inside a flex widget.

  • proper-from-environmentproper_from_environmentrecommended

    Require fromEnvironment to be evaluated in a const context.

  • proper-super-init-stateproper_super_init_staterecommendedflutter

    Require super.initState() to be the first statement of a resolved Flutter State subclass's initState override.