aagarwal1012 / Liquid-Pull-To-Refresh

🔁 A custom refresh indicator for flutter.
https://pub.dev/packages/liquid_pull_to_refresh
MIT License
1.24k stars 92 forks source link

Cannot trigger refresh programmatically using the global key. The method 'show' was called on null. #37

Closed pavelhov closed 4 years ago

pavelhov commented 4 years ago

Describe the bug Calling _refreshIndicatorKey.currentState.show(); throws an error "The method 'show' was called on null."

To Reproduce

Just run the example app for the library and button that executes _refreshIndicatorKey.currentState.show(); Steps to reproduce the behavior

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Flutter: v1.9.1+hotfix.6

I replaced the LiquidPullToRefresh to Flutter's own RefreshIndicator and the show method worked as expected.

juancastellano115 commented 4 years ago

Same happens to me

AadumKhor commented 4 years ago

Hi @pavelhov . The line of code you're referring to _refreshIndicatorKey.currentState.show() works fine in the example app. You can check the _handleRefresh method inside of which we are handling this request. Do you have a specific example where you are facing an issue? Let me know the exact steps to replicate it. Closing this issue for now.

pavelhov commented 4 years ago

Hi @AadumKhor . I have no idea how you got that line of code to work without getting the null pointer exception. To explain, that line of code refers to the ability to trigger the refresh function and animation programmatically and not strictly through ui. In the example app the way to reproduce the bug would be to first pull down and see the refresh animation, then once the animation is complete you get a bottom snack bar saying "Refresh complete" with a retry button next to it. Tapping the retry button will give you the null pointer exception. Now the fix is quite simple. if you look at the declaration of the _refreshIndicatorKey it is of type GlobalKey. However, your refresh indicator state is actually of type _LiquidPullToRefreshState. Hence your global key is inconsistent with the type of your state object hence, flutter does not have a way to find it in the widget tree. You will need to make _LiquidPullToRefreshState not private so that you can change the global key in your example to the appropriate type.

AadumKhor commented 4 years ago

This was an error at my end. I did not update the actual project and made the changes in the forked project. I'll merge the fix.