Home > Back-end >  how to manage the state correctly using getbuilder
how to manage the state correctly using getbuilder

Time:12-01

i am facing a problem on managing the state using GetBuilder

i am creating an app looks like twitter you can publish a post and like a post ...etc

enter image description here

i have this Row Contains

  • favorite icon
  • comments icon

i want whenever the user clicks the favorite icon the counter change from zero to 1 and it turn its color to red

if he re-Clicked the icon it turns back (white icon and the counter get to zero).

everything logically works fine but i need to **refresh the page every time if there is changes **

here is the code

`

 GetBuilder<PostContainerController>(
                      builder: (controller) {
                        return Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Text(likes.length.toString()),
                                IconButton(
                                  icon: Icon(
                                    Icons.comment_outlined,
                                  ),
                                  onPressed: () {},
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(likes.length.toString()),
                                IconButton(
                                  icon: likes.contains(currentusername)
                                      ? Icon(
                                          Icons.favorite,
                                          color: Colors.red,
                                        )
                                      : Icon(
                                          Icons.favorite_outline,
                                        ),
                                  onPressed: () async {
                                    print(likes.toString());
                                    if (likes.contains(currentusername)) {
                                      controller.unlikePostcontroller(id);
                                    } else {
                                      controller.likePostcontroller(id);
                                    }
                                  },
                                ),
                              ],
                            ),
                          ],
                        );
                      },
                    )

`

my controller

enter image description here

if i create a variable in controller class and use it in getbuilder scope then all posts would have the same value, i want each post get unique value from Firebase Firestroe

thank you

CodePudding user response:

the Getx package offers a solution for this, by setting those properties:

 GetBuilder<PostContainerController>(
   init: PostContainerController(),
   global: false,
   builder: (controller) {
     return Row(
       mainAxisAlignment: MainAxisAlignment.spaceBetween,
         children: [
            Row(
 //....

By setting the global property to false, we're telling GetBuilder that each widget called from it should be unique and its state also is unique, so each one will need a unique GetxController, so for that, you will need to define that unique GetxController for each one with the init property.

  • Related