Flutter Like Button Animation Not Working After Adding Method

0 votes

I am using the like_button package from this repository (https://pub.dev/packages/like_button) in my Flutter application. The like button animation was working fine under normal conditions. However, after I added a method to be executed when the button is pressed, the animation stopped working as expected.

I can confirm that the method is working properly, but the frontend animation of the like button is not working. It seems to be related to this method, as the animation works fine without it.

Here is the relevant code:

  Future<bool> onLikeButtonTapped(bool isLiked) async {
    print('pressed');

    await toggleFavorite();
    print(success);

    return !isLiked;

    /// if failed, you can do nothing
    // return success? !isLiked:isLiked;
  }
}
Future<void> toggleFavorite() async {
    if (favorites.contains(current)) {
      favorites.remove(current);
    } else {
      favorites.add(current);
    }
    print(favorites);
    notifyListeners();
  }

The toggleFavorite() function, which is an asynchronous function that performs some operation. After adding this method, the like button animation stopped working. But the function is working properly and I can see the output.

Apr 12, 2023 in Flutter by sarit
• 1,830 points
901 views

1 answer to this question.

0 votes

It seems like the issue might be related to the fact that the toggleFavorite() method is an asynchronous function. When you use await inside the onLikeButtonTapped method, it might be causing the animation to halt until the toggleFavorite() function is completed.

To fix this, you can try removing the await keyword before toggleFavorite() and instead using then() to handle the result of the function.

To know more about Flutter, join our Flutter Course today.

answered Apr 12, 2023 by Tanishqa
• 1,170 points

Related Questions In Flutter

0 votes
1 answer

flutter app not running on ios simulator after adding firebase notification

The error message you provided seems to ...READ MORE

answered Apr 10, 2023 in Flutter by vishalini
5,327 views
0 votes
1 answer

flutter, avatarGlow is not working at all with fluoatingbutton

Based on the code you provided, it ...READ MORE

answered Apr 14, 2023 in Flutter by Anitha
737 views
0 votes
1 answer
0 votes
1 answer

Flutter get context in initState method

To execute code after the widget is ...READ MORE

answered Mar 27, 2023 in Flutter by anonymous
7,193 views
0 votes
1 answer

Create a rounded button / button with border-radius in Flutter

To create a rounded button with border-radius ...READ MORE

answered Mar 1, 2023 in Android by vishalini
32,005 views
0 votes
1 answer

Flutter - Wrap text on overflow, like insert ellipsis or fade

Container( padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 18.0), ...READ MORE

answered Mar 1, 2023 in Android by vishalini
1,351 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

How to create a circle icon button in Flutter?

To create something similar to a FloatingActionButton, ...READ MORE

answered Aug 23, 2023 in Flutter by anonymous
• 140 points
14,561 views
0 votes
1 answer

Flutter buildproblem

It seems like there's an issue with ...READ MORE

answered Apr 12, 2023 in Flutter by Tanishqa
• 1,170 points
3,396 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP