fl-query
fl-query copied to clipboard
Reset Query in 1.0.0
Hello,
Firstly thanks a lot for your work on this package; it has been immensely helpful for me.
I've noticed that the "reset" function for queries is no longer in the documentation. Is it no longer possible to do this? The function still seems to be present in the library, however, it seems to have no effect. Is it possible to reset queries for my use case?
Thanks!
I think I accidentally didn't add the reset method in the docs.
But reset shouldn't be called in the dispose or cleanup function of useEffect as it can cause Widget build errors. Calling reset while unmounting will make the useQuery to set states while rebuilding, which can lead to memory leaks.
If you need to run a new query on each mount (page/dialog open) you should supply a unique queryKey on each mount (a unique id which won't change across re-renders) and on unmount or pop use the QueryClient.of(context).cache.removeQuery
Widget build(context){
final query = useQuery("pages/${pageNumber}", _fetchPage);
final queryClient = QueryClient.of(context);
useEffect((){
return (){
queryClient.cache.removeQuery(query);
}
}, []);
//... UI stuff
}
If you don't have a unique id you can just create one on each render
Widget build(context){
final uniqId = useMemoized(()=> Uuid().v4(), []);
// ...other stuff
}
Thank you for your help, the removeQuery() seems to correctly reset this. Is this safe to do while the query is executing, otherwise is there a way to stop the query if it's running first before doing this?