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

beforeDispose or beforeInvalidate hook, happen before invalidate #3570

Open
samderlust opened this issue May 22, 2024 · 5 comments
Open

beforeDispose or beforeInvalidate hook, happen before invalidate #3570

samderlust opened this issue May 22, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@samderlust
Copy link

when invalidate a Future provider (that gets data) after updating remote data. The request happens so fast that sometimes get request returns not updated data.

It would be nice if there is a hook before dispose or invalidate, that I can add a wait time before the provider invalidates to ensure the data getting back is updated

@rrousselGit
Copy link
Owner

Could you be more specific? I don't really understand the problem, not what is suggested here.

Some examples of the problem and the suggestion would be great.

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels May 22, 2024
@samderlust
Copy link
Author

samderlust commented May 22, 2024

@rrousselGit

final getProvider = FutureProvider<String>(
  (ref) async {
    // get remote data
    return '';
  }
);

//in other function somewhere esle
void updatedata()async{
  // await update data
  ref.invalidate(getProvider)
}

so in this case ref.invalidate(getProvider) will trigger get remote data right after the update. Sometime it doesn't leave enough time for server to update data all the way through. so it returns un updated data.
A hook could be something like this.

final getProvider = FutureProvider<String>(

  (ref) async {
    ref.beforeInvalidate(() async{
      await Future.delayed(const Duration(seconds: 1));
    })
    // get remote data
    return '';
  }
);

This could be useful in some other usecases I think

@rrousselGit
Copy link
Owner

I'm sceptical about this. If the provider was invalidated, to me it should rebuild immediately the next time it is used.
Adding an indirect delay like that would be confusing. It'd be hard to know why the refresh isn't happening.

To be, it sounds like you want to do:

//in other function somewhere esle
void updatedata()async{
  await Future.delayed(const Duration(seconds: 1));
  ref.invalidate(getProvider)
}

@rrousselGit
Copy link
Owner

In fact, the fact that you need this delay is weird to me.

Your server should be up-to-date. If it isn't, that means you didn't await something.

@samderlust
Copy link
Author

yeah some time the request sent too fast and server still cache I think.

//in other function somewhere esle
void updatedata()async{
  await Future.delayed(const Duration(seconds: 1));
  ref.invalidate(getProvider)
}

This works most of the time. but Sometimes in the UI ref.invalidate(getProvider) scatters other places. so would a bit inconvenient to add a wait time to other places like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants