like_button
like_button copied to clipboard
isLiked sync issue
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 https://github.com/fluttercandies/like_button/blob/8ecb015abba463e34fb323f9e13f8a76eb54270b/lib/src/like_button.dart#L401
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
https://github.com/fluttercandies/like_button/blob/8ecb015abba463e34fb323f9e13f8a76eb54270b/lib/src/like_button.dart#L147
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.