skip to Main Content

i could not manage the state when i used Get.bottomSheet even when i wrap it with GetBuilder(), and use update() function.

here is the code

this is the function that show the comments in a bottomsheet.

  commentFunction(context, id, comments) {
Get.bottomSheet(CustomPageForComments(
    postid: id,
    sendCommentFunction: () {
      sendComment(id, commentController.text);
    },
    controllerForCommentField: commentController,
    comments: comments));}

the CustomPageForComments() is a simple page that used stream in order to get the comments from database.

and finally this is the send comment function

  sendComment(String id, String message) {
commentController.clear();
DocumentReference setter =
    FirebaseFirestore.instance.collection("Posts").doc(id);

setter.update({
  "comments": FieldValue.arrayUnion([
    {
      "username": currentusername,
      "message": message,
      "time": DateTime.now(),
      "id": formatDate(DateTime.now(),
          [yyyy, ":", mm, ":", dd, ":", hh, ':', nn, ":", ss]),
    }
  ])
});
update();}

i want to refresh the bottom sheet when i use the send comment function, so the user can see his comment without re- opening the bottom sheet.

when you close the bottom sheet and re-open it, it actually update

how can i manage that?

also i wanted to ask, is it true to use stream Builder in comments instead of Future Builder?

thank you

2

Answers


  1. To change the state inside a bottom sheet you can use StatefulBuilder

    Get.bottomSheet(
       StatefulBuilder(
             builder: (BuildContext context, StateSetter setState) {
        return Container(
          height: heightOfModalBottomSheet,
          child: RaisedButton(onPressed: () {
            setState(() {
              // Change state here
            });
          }),
        );
      });
    )
    

    If you want the comments to update in real time (could incur higher costs due to reads) use a Stream Builder , to fetch data just once use FutureBuilder

    Login or Signup to reply.
  2. All works fine with update() in bottomSheet.
    Make sure for proper use GetBuilder. You need refresh inner widget, not the Get.bottomSheet function. Your code should look like this:

                  Get.bottomSheet(
                    GetBuilder<CommentController>(builder: (cLogic) {
                    return Text('Comments count: ${cLogic.count}');
                  })
                 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search