-
Hi, I'm trying to rebuild a ConsumerStatefulWidget while passing new parameters to it and resetting the state. The widget: class SearchPage extends ConsumerStatefulWidget {
final String query;
const SearchPage({this.query = '', Key? key}) : super(key: key); auto_route router function - SearchRoute => SearchPage: AutoRouter.of(context).popAndPush(
SearchRoute(query: searchTerm),
); The provider: final searchNotifierProvider = StateNotifierProvider.autoDispose<SearchNotifier, SearchState>(
(ref) => SearchNotifier(
ref.watch(searchRepositoryProvider),
),
); I was hoping that popAndPush would force the provider to autoDispose but it doesn't. Even widget.query keeps it's previous data. I'm pretty sure it's a simple thing that I missed :) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
You can either refresh the provider: (Old Riverpod |
Beta Was this translation helpful? Give feedback.
-
Providers won't be disposed if they are immediately reused So as Tim mentioned, you'd need to call "ref.refresh" |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for your quick replies, guys. It's good to know that ref.refresh is the right way to reset my state. However, I think the issue must be with AutoRouter.of(context).popAndPush(
SearchRoute(query: searchTerm),
); The Thank you again for your time. |
Beta Was this translation helpful? Give feedback.
-
I found a way to refresh the provider (instead of reloading the widget) and rerun the search function with the new searchTerm. ref.refresh<SearchState>(searchNotifierProvider);
ref.read(searchNotifierProvider.notifier).getNextSearchedPage(currentTerm); Thanks again. |
Beta Was this translation helpful? Give feedback.
-
By the way, the issue with popping and re-pushing the page was that I had to use |
Beta Was this translation helpful? Give feedback.
By the way, the issue with popping and re-pushing the page was that I had to use
pushAndPopUntil(...)
instead ofpopAndPush(...)
. So,ref.refresh
thenpushAndPopUntil
works well.