Don’t use one refreshController to multiple SmartRefresher, It will cause some unexpected bugs mostly in TabBarView
‘package:pull_to_refresh/src/smart_refresher.dart’:
package:pull_to_refresh/src/smart_refresher.dart:1
Failed assertion: line 608 pos 12: ‘_refresherState == null’
The issue was using one refreshController to multiple SmartRefresher. The solution was quite simple: i.e Define another instance for another smart refresher.
However, my attention was focused on TabBarView, GridView, Silvers, etc Because many on StackOverflow and other forums mostly discussed those issues focusing on TabBarView.
I wasted a lot of time.
Issue:
I was using two SmartRefresher but one instance of RefreshController. The same goes for the separate _OnRefresh Function.
RefreshController _refreshController = RefreshController(initialRefresh: false);
Future _onRefresh() async {
if (Helper.checkConnectivity(context)) {
await Provider.of<ProductProvider>(context, listen: false)
.getCategoryProducts(
context: context, categorySlug: _selectedCategory);
}
_refreshController.refreshCompleted();
}
SliverFillRemaining(
child: SmartRefresher(
controller: _refreshController,
enablePullDown: false,
enablePullUp: false,
onRefresh: _onRefresh,
child: PageView(
...
child: SmartRefresher(
controller: _refreshController,
enablePullDown: true,
enablePullUp: false,
onRefresh: _onRefresh,
child: GridView.count(
shrinkWrap: true,
primary: false,
Even When I created two different instances refreshController and _refreshController and passed them to the controller in those two SmartRefresher. _onRefresh function was creating an issue. I was getting Future(dynamic) to void()? …. something like that. I forgot.
Solution
Like I said earlier, the solution was quite simple which is to pass the different instances of RefreshController to different SmartRefresher.
Another thing, rather than creating a separate function _OnRefresh outside, I directly created the function inside SmartRefresher.
RefreshController _refreshController = RefreshController(initialRefresh: false);
Widget build(BuildContext context) {
S s = S.of(context);
RefreshController refreshController = RefreshController();
return Scaffold(
...
SliverFillRemaining(
child: SmartRefresher(
controller: refreshController,
enablePullDown: false,
enablePullUp: false,
onRefresh: () async {
if (Helper.checkConnectivity(context)) {
await Provider.of<ProductProvider>(context,
listen: false)
.getSelectedCategory(
refreshController.refreshCompleted();
...
Widget buildCategoryProducts(
return Container(
padding: EdgeInsets.symmetric(horizontal: 8),
child: SmartRefresher(
controller: _refreshController,
enablePullDown: true,
enablePullUp: false,
onRefresh: _onRefresh,
This worked for me. I was using TabBarView, GridView, and Silvers, and even then the code worked fine.
If it doesn’t work out for you you can try the solutions down below:
If you like to know more about Refresher, you can checkout this youtube video: Pull to Refresh and Refresh Indicator Tutorial