0

Trying a 'like button' within a Future builder with many other widgets as below , enter image description here

onPressed: () {
    if (aleadyLiked.length > 0) {
        unlike(profileId);
     } else {
        like(profileId);
     }
   setState(() {});
 },

And this is how my future builder starts,

@override
Widget build(BuildContext context) {
return FutureBuilder(
  future: getProfile(profileId), 
  builder: (context, snapshot) {
  =======Other widgets here======
  }

Issue is onPressed of the like icon-button I am doing the setState() which is causing the whole Future builder to reload , Is there a way just to update the Like Button and the Like count , I was thinking to use some client side counter logic which callbacks to actual DB updates .Please help.

Loading Profile part on initState() can be achieved, but how to handle updating and reflecting 'Likes' , can that Like-button region alone be reloaded ?

sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
  • 1
    Does this answer your question? [How to deal with unwanted widget build?](https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build) – LonelyWolf Jun 27 '20 at 20:32
  • Nope , this basically solves/instructs to load profile on the initState() , that is fine , my question is , after profile load how to handle the 'Likes' update , can that Like-button region alone be reloaded ? – sajanyamaha Jun 28 '20 at 10:14

2 Answers2

0

You should not get this User Profile like this, but what you can do rather, you can get the user profile inside the initState, and before data is not loaded you can show either loader of something.

Something like this...

  User _user;
  
  Future<User> getUserProfile(profileId) async{
    ///Todo : Implementation of get User Profile
  }
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUserProfile("userId").then((user){
      setState(() {
        _user =user;
      });
    })
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: this._user == null ? CircularProgressIndicator() : UserWidget(),
    );
  }
Maaz Aftab
  • 238
  • 2
  • 9
0

So this is how I finally achieved it ,

void initState() {
 getProfile(profileId).then((user){
  setState(() {
    _profile =user;
    _counter =_profile.profilelikes.length;
    _isAlreadyLiked=_profile.allreadyliked.length > 0;
  });
 });
 super.initState();
}

And OnPressed() is

                                                            onPressed: () {
                                                              if (_isAlreadyLiked) {
                                                                unlike(profileId);
                                                                setState(() {
                                                                  _counter--;
                                                                  _isAlreadyLiked=false;
                                                                });
                                                              } else {
                                                                like(profileId);
                                                                setState(() {
                                                                  _counter++;
                                                                  _isAlreadyLiked=true;
                                                                });
                                                              }
                                                            },

Downside : The likes by other users will reflect on Wiget reload only, which is fine for me , for now.

sajanyamaha
  • 3,119
  • 2
  • 26
  • 44