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
Closed

[riverpod_lint] Add new lint incorrect_usage_of_ref_method #2947

wants to merge 11 commits into from

Conversation

charlescyt
Copy link
Contributor

Try to close #2445.

This lint turns out to be more challenging than I thought. Although It may not cover all the edge cases, I hope it will still be a good starting point to build upon.

@codecov
Copy link

codecov bot commented Oct 3, 2023

Codecov Report

Merging #2947 (e8741ce) into master (3b8ca72) will increase coverage by 0.04%.
Report is 2 commits behind head on master.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2947      +/-   ##
==========================================
+ Coverage   95.24%   95.29%   +0.04%     
==========================================
  Files          53       53              
  Lines        2252     2274      +22     
==========================================
+ Hits         2145     2167      +22     
  Misses        107      107              
Files Coverage Δ
packages/riverpod/lib/src/common.dart 100.00% <100.00%> (ø)

@rrousselGit
Copy link
Owner

Hello!
I'm not a fan of having a single lint for all methods.

I think we should have one lint per ref method. So folks can enable/disable what they wish

});

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.


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(...)


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, ...);
}

@rrousselGit
Copy link
Owner

One important case to handle for WidgetRef.watch specifically is nested builders:

build(context, ref) {
  return ListView.builder(
    builder: (context, index) {
      ref.watch(...); // This is fine
    }
  );
}

This shouldn't be allowed for WidgetRef.listen.

@rrousselGit
Copy link
Owner

I would also dissociate the lints based on which ref they are coming from.
WidgetRef.watch is not the same as Ref.watch

@charlescyt
Copy link
Contributor Author

Hello! I'm not a fan of having a single lint for all methods.

I think we should have one lint per ref method. So folks can enable/disable what they wish

No problem. I could refactor it to multiple lints.

@charlescyt
Copy link
Contributor Author

I would also dissociate the lints based on which ref they are coming from. WidgetRef.watch is not the same as Ref.watch

Yeah. I am already doing that, e.g. using addRefWatchInvocation and addWidgetRefWatchInvocation.

@rrousselGit
Copy link
Owner

Yeah. I am already doing that, e.g. using addRefWatchInvocation and addWidgetRefWatchInvocation.

By that I meant having a different lint

@charlescyt
Copy link
Contributor Author

hmm... the lint is harder than I thought.
I am going to close the PR for now.

@charlescyt charlescyt closed this Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn against incorrect ref.method(provider) usage
2 participants