Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[riverpod_lint] Add new lint incorrect_usage_of_ref_method #2947

Closed
wants to merge 11 commits into from
86 changes: 86 additions & 0 deletions packages/riverpod_lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Riverpod_lint adds various warnings with quick fixes and refactoring options, su
- [avoid\_ref\_inside\_state\_dispose](#avoid_ref_inside_state_dispose)
- [notifier\_build (riverpod\_generator only)](#notifier_build-riverpod_generator-only)
- [async\_value\_nullable\_patttern](#async_value_nullable_patttern)
- [incorrect\_usage\_of\_ref\_method](#incorrect_usage_of_ref_method)
- [All assists](#all-assists)
- [Wrap widgets with a `Consumer`](#wrap-widgets-with-a-consumer)
- [Wrap widgets with a `ProviderScope`](#wrap-widgets-with-a-providerscope)
Expand Down Expand Up @@ -643,6 +644,91 @@ switch (...) {
}
```

### incorrect_usage_of_ref_method

Warn if the `ref.watch`/`ref.read`/`ref.listen`/`ref.listenManual` methods are used incorrectly.

**Good**:

```dart
final fnProvider = Provider<int>((ref) {
ref.watch(provider);
ref.listen(provider, ...);
return 0;
});

class MyNotifier extends Notifier<int> {
int get _readGetter => ref.read(provider);

@override
int build() {
ref.watch(provider);
ref.listen(provider, ...);
return 0;
}

void someMethod() {
ref.read(provider);
}
}

void initState() {
ref.listenManual(provider, ...);
}

Widget build(ctx, ref) {
ref.watch(provider);
ref.listen(provider, ...);

return Button(
onPressed: () {
ref.read(provider);
ref.listenManual(provider, ...);
}
);
}
```

**Bad**:

```dart
final fnProvider = Provider<int>((ref) {
ref.read(provider); // use watch instead
return 0;
});

class MyNotifier extends Notifier<int> {
int get _watchGetter => ref.watch(provider); // use read instead
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recommend making such getters at all. They can introduce bugs implicitly, because the getter may be used in multiple places where some places want read and others want watch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I will remove it.


@override
int build() {
ref.read(provider); // use watch instead
return 0;
}

void someMethod() {
ref.watch(provider); // use read instead
ref.listen(provider, ...); // place listen in build instead
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recommendation would be to use listenManual

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is inside a Notifier, ref is not WidgetRef.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed that. The there's no issue with calling listen in methods.
As long as the return value is used to cancel subscription.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. So this is a valid use case of Ref.listen.

void someMethod(){
  sub?.close();
  sub = ref.listen(provider, ...);
}

}
}

void initState() {
ref.listen(provider, ...); // use listenManual instead
}

Widget build(ctx, ref) {
ref.read(provider); // use watch instead
ref.listenManual(provider, ...); // use listen instead
Copy link
Owner

@rrousselGit rrousselGit Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no strong requirement against this ... as long as they use the returned subscription and deal with it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instance they could do

sub?.cancel();
sub = ref.listenManual(...)


return Button(
onPressed: () {
ref.watch(provider); // use read instead
ref.listen(provider, ...); // use listenManual instead
}
);
}
```

## All assists

### Wrap widgets with a `Consumer`
Expand Down
2 changes: 2 additions & 0 deletions packages/riverpod_lint/lib/riverpod_lint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'src/lints/avoid_manual_providers_as_generated_provider_dependency.dart';
import 'src/lints/avoid_public_notifier_properties.dart';
import 'src/lints/avoid_ref_inside_state_dispose.dart';
import 'src/lints/functional_ref.dart';
import 'src/lints/incorrect_usage_of_ref_method.dart';
import 'src/lints/missing_provider_scope.dart';
import 'src/lints/notifier_build.dart';
import 'src/lints/notifier_extends.dart';
Expand All @@ -39,6 +40,7 @@ class _RiverpodPlugin extends PluginBase {
const AvoidRefInsideStateDispose(),
const NotifierBuild(),
const AsyncValueNullablePattern(),
const IncorrectUsageOfRefMethod(),
];

@override
Expand Down
Loading
Loading