首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在Flutter Streambuilder中构建Like/Like Button -不起作用

问尝试在Flutter Streambuilder中构建Like/Like Button -不起作用
EN

Stack Overflow用户
提问于 2020-08-11 14:34:36
回答 1查看 966关注 0票数 0

我正在尝试在我的应用程序中构建的新闻提要中获得一个like/like按钮。我使用的是streambuilder。

下面是按钮的代码:

代码语言:javascript
复制
FlatButton(
                              child: Container(
                                child: Column(
                                  children: <Widget>[
                                    Icon(
                                      Icons.thumb_up,
                                      color: Colors.black,
                                    ),
                                    Text(
                                      'Like',
                                      style: TextStyle(fontSize: 12.0),
                                    ),
                                  ],
                                ),
                              ),
                              onPressed: isPostLiked == true ? () {
                                _firestore.collection('posts').document(postID).updateData({'likeCount' : postLikeCount - 1, 'likedBy' : FieldValue.arrayRemove(['${loggedInUser.email}'])});


                                isPostLiked = false;
                                print('USER HAS UNLIKED THIS POST');

                              } : () {
                                _firestore.collection('posts').document(postID).updateData({'likeCount' : postLikeCount + 1, 'likedBy' : FieldValue.arrayUnion(['${loggedInUser.email}'])});

                                isPostLiked = true;
                                print('USER HAS LIKED THIS POST');

                              },
                            ),

isPostLiked是一个布尔值,我最初将其设置为false bool isPostLiked = false;。

我的问题是,它只让用户喜欢帖子。它不会让用户喜欢这个帖子。似乎只是没有更新isPostLiked = true,这似乎是我要发牢骚的地方。有什么想法吗?

有关更多上下文,下面是嵌套FlatButton的Streambuilder小部件的一些代码:

代码语言:javascript
复制
StreamBuilder<QuerySnapshot>(
              stream: _firestore
                  .collection('posts')
                  .orderBy('date', descending: true)
                  .snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Center(
                    child: CircularProgressIndicator(
                      backgroundColor: Colors.lightBlueAccent,
                    ),
                  );
                }
                final posts = snapshot.data.documents;
                List<Widget> postWidgets = [];
                for (var post in posts) {
                  bool isPostLiked = false;
...

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2020-08-11 14:54:02

尝试将onpressed更改为以下内容:

代码语言:javascript
复制
onPressed: () {
      if (isPostLiked) {
        // add your firebase code
        print('unlike');
      } else {
        // add your firebase code
        print('like');
      }
      isPostLiked = !isPostLiked;
    }

我在dartpad上试过,每次按下时变量都会改变。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63352721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档