Implement optimistic UI update in LikeButton with rollback on async failure #87
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR updates the LikeButton widget to support optimistic UI updates.
What changed:
• When the user taps the button, the animation starts immediately and the like/unlike state updates right away.
• An external asynchronous call (e.g., to a backend or repository) is performed to confirm the action.
• If the response is negative or an error occurs, the widget automatically reverts to the previous state, restoring the like count and animation.
• The callback mechanism was modified to enable this rollback logic.
Benefits:
• Improves app responsiveness and perceived fluidity with optimistic UI.
• Automatically handles error cases without leaving inconsistent visual states.
• Makes integration with potentially slow or failing API calls easier.
⸻
How to test
• Tap the LikeButton and verify the animation runs immediately.
• Simulate a negative response or error in the onTap/onToggleLike function to confirm rollback works.
• Check the like count updates correctly and reverts if necessary.