【问题标题】:Like and unlike feature in flutter firebaseFlutter Firebase 中的喜欢和不喜欢的功能
【发布时间】:2021-07-10 03:19:18
【问题描述】:

我正在尝试使用 Firebase 在颤振上构建类似的功能。我能够使用事务存储和删除类似的东西。我坚持的是,我如何在前端表示类似的状态?

(_isLiked == false)
        ? IconButton(
            iconSize: Sizes.s35,
            color: Colors.black,
            icon: Icon(Icons.favorite_outline),
            onPressed: () {
              setState(() {
                _isLiked = true;
                final like = LikeData(
                  campaingID: donation.campaignID,
                  dateTime: Timestamp.now(),
                  userId: user.uid,
                  likeId: DateTime.now().toString(),
                  like: _isLiked,
                );
                likeService.newLike(donation, like);
              });
            },
          )
        : IconButton(
            iconSize: Sizes.s35,
            color: Colors.green,
            icon: Icon(Icons.favorite),
            onPressed: () {
              setState(() {
                likeService.deleteLike(donation, user);
              });
            },
          ),

这就是我现在将其表示为硬编码的_isliked 布尔值的方式。如何从 Firebase 调用状态?

【问题讨论】:

    标签: firebase flutter firebase-realtime-database google-cloud-firestore


    【解决方案1】:

    您可能需要添加一个 StreamBuilder 来监听喜欢/不喜欢的实时变化,我已经为您提供了以下解决方案。

    Widget likeWidget(String postId) {
    return Column(
      children: [
        SizedBox(
          height: Get.height * 0.022,
          width: Get.width * .15,
          child: StreamBuilder(
              stream: FBCollections.postLikes
                  .where("post_id", isEqualTo: postId)
                  .where("user_id", isEqualTo: userData.userEmail)
                  .snapshots(),
              builder:
                  (context, AsyncSnapshot<QuerySnapshot> snapshotCheckLike) {
                if (!snapshotCheckLike.hasData) {
                  return Container();
                } else {
                  return snapshotCheckLike.data.docs.isEmpty
                      ? IconButton(
                          icon: Icon(Icons.favorite_outline),
                          onPressed: () {
                            final like = LikeData(
                              campaingID: donation.campaignID,
                              dateTime: Timestamp.now(),
                              userId: user.uid,
                              likeId: DateTime.now().toString(),
                              like: true,
                            );
                            likeService.newLike(donation, like);
                          },
                        )
                      : IconButton(
                          iconSize: Sizes.s35,
                          color: Colors.green,
                          icon: Icon(Icons.favorite),
                          onPressed: () {
                            setState(() {
                              likeService.deleteLike(donation, user);
                            });
                          },
                        );
                }
              }),
        ),
      ],
    );
    

    }

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多