fluttercandies/like_button

isLiked sync issue

Closed this issue · 4 comments

Thank you for the awesome animation button package. I've very appreciated your effort.
Recently, I've noticed somewhat race condition issue, which prevents the animation to work correctly.

The following is my pseudocode sample.

bool _isOn;
...

Widget build(BuildContext context) {
  return LikeButton(
    isLiked: _isOn,
    onTap: (isOn) async {
      // Update _isOn value
      return !isOn;
    }
  );

Normal working flow should be

onTap called
-> Update _isOn value
-> onTap returned
-> animation
-> LikeButton didUpdateWidget called from isLiked value update.

However, this will not be guaranteed every time.
So the flow could be

onTab called
-> Update _isOn value
-> LikeButton didUpdateWidget called from isLiked value update
-> the Icon changed from _isLiked value update
-> onTap returned
-> No animation because internal _isLiked already updated

The issue is caused because the following like

widget.onTap(_isLiked ?? true).then((bool isLiked) {

Is there a reason onTap requires Future<bool> instead of bool?

I think a simple change to bool will remove the race condition.

1.sometime,we should send a request await response back, then we decide change isLiked or not.
2.we shouldn't change _isOn at tap, _isOn is Initial state

@zmtzawqlp Thank you for your answer.
if _isOn the variable isLiked is only the initial value.
We should change it to initialValue or something similar and also
we shouldn't do

_isLiked = widget.isLiked;

we should send a request await response back, then we decide change isLiked or not.

this makes sense, but has one issue:

my like buttons are linked up to state, so when the request completes, isLiked will update and immediately interrupt the animation. if I unlink them from state, they will not update if I trigger a like from anywhere else.

So currently, I have a really ugly mechanism where the state update will wait until at least 1 second is passed, to update the state.

The package is great, but this issue makes me give up on using it. :(