Reputation: 303
I have a 'homeScreenViewController' provider and it is a StateNotifierProvider. The state it provides is a class with multiple variables. I want my button widget to rebuild only when a particular variable 'isLoadMoreLoading' changes. But when I use like below everytime any variable is updated, my button rebuilds. How to achieve this? I am using flutter_riverpod: ^1.0.0.dev6
Consumer(
builder: (context, ref, child) {
var _isLoadMoreLoading = ref.watch(homeScreenViewController).isLoadMoreLoading;
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
if (!_isLoadMoreLoading) {
await ref
.read(homeScreenViewController.notifier)
.loadMorePokemons();
WidgetsBinding.instance!
.addPostFrameCallback((_) {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 100),
curve: Curves.easeInOut);
});
}
},
child: _isLoadMoreLoading
? Container(
width: 50.0,
height: 1.0,
child: LinearProgressIndicator(
color: Colors.black,
minHeight: 1.0,
),
)
: Text('Load More Pokemon'),
),
],
)
],
);
},
),
Upvotes: 3
Views: 3432
Reputation: 93
With the release of version 1.0.0, ref.watch now supports filtering rebuilts using myProvider.select((value) => ...)
If you have used flutter_bloc, we can say that it's riverpod equivalent of Bloc's buildWhen.
So, in your case we can do something like this:
var _isLoadMoreLoading = ref.watch(homeScreenViewController.select((viewController) => viewController.isLoadMoreLoading));
This will allow consumer widget to rebuilt only when isLoadMoreLoading variable updates its value.
Can be referred here: https://pub.dev/packages/riverpod/versions/1.0.0-dev.6/changelog
Upvotes: 5