Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 记住在颤振中重新打开后Like按钮的状态_Firebase_Flutter - Fatal编程技术网

Firebase 记住在颤振中重新打开后Like按钮的状态

Firebase 记住在颤振中重新打开后Like按钮的状态,firebase,flutter,Firebase,Flutter,我希望我的应用程序在关闭/重新打开“Like Button”后记住它的状态。我使用的是firebase db和Flatter。我假设您使用的是Cloud Firestore,那么您可能需要在数据库中正确地构建数据,但我会给您一个示例来让事情顺利进行 让我们假设Firestore上的文档结构类似于posts/postId,其中posts是集合,postId表示posts集合中的通用文档,其中每个post都有一个like按钮 现在,我们可以将Firestore上的用户文档结构如下: uid: //(

我希望我的应用程序在关闭/重新打开“Like Button”后记住它的状态。我使用的是firebase db和Flatter。

我假设您使用的是Cloud Firestore,那么您可能需要在数据库中正确地构建数据,但我会给您一个示例来让事情顺利进行

让我们假设Firestore上的文档结构类似于posts/postId,其中posts是集合,postId表示posts集合中的通用文档,其中每个post都有一个like按钮

现在,我们可以将Firestore上的用户文档结构如下:

uid: //(here goes the postId, you should also name the document by the same postId)
likedBy: //(This is an array of userId's, where if the user likes this post his Id will be placed here)
请注意,您需要对用户进行身份验证并获取其Id,如果这不是应用程序中的一项功能,那么您可能需要使用共享首选项,正如有人所说的那样

然后,在flatter中,您需要检查用户是否喜欢该帖子。我不会告诉您如何构造应用程序的体系结构,但要了解用户是否喜欢带有“like”按钮的帖子,可以使用以下异步Dart代码:

布尔是最受欢迎的; Future docSnapshot=Firestore.instance.collection'posts'。documentpostId.get; DocumentSnapshot doc=等待docSnapshot; 如果单据['likedBy'].containerserid{ IsPostlike=true; }否则{ ispost=false; }
谢谢阿里·阿明先生。我遵循他的指导方针。我使用此代码来更新“likedby”字段

Firestore.instance.runTransaction((transaction) async {
        DocumentSnapshot freshSnap = await transaction.get(document.reference);
        await transaction.update(freshSnap.reference, {
          'vote': freshSnap['vote'] + 1,
        });
        List<String> users = ["4"]; //userId
        await transaction.update(freshSnap.reference, {
          'likedby': FieldValue.arrayUnion(users),
        });
      });

这与Firestore无关。但是,如果有人来到这里了解如何使用SQL、MySQL数据库实现这一点,请参见下面的示例。我将向新手解释这一点

变量:这家伙会管理所有的事情`

bool _isLiked = false;
Widget initState:在这里,您应该检查后端的状态。如果用户已经喜欢它,您可以显示他已经喜欢它

@override
  void initState() {
    super.initState();

    if(widget.replyData.isLiked == 1){
      setState(() {
        _isLiked = true;
      });
    }
  }
类似按钮:

最终结果:


“相似按钮的状态存储在哪里?我将使用“共享首选项”,但我不知道它是否合适。@Hasini这取决于“相似按钮”的用途是什么?它是应用程序的关键部分吗?如果是这样的话,我想说共享偏好是可以接受的。否则,您应该考虑如何存储/存储其他数据。这意味着当用户卸载应用程序或清除其数据时,所有SharedReference都将丢失。所以这取决于你想要什么。如果用户与其他人交互,你应该考虑使用云DB或者甚至在线JSON文件。否则,您可以只使用SharedReferences或本地存储。请编辑您的问题,解释您正在制作什么类型的应用程序或您正在尝试完成什么。@AlexLushiku我正在开发一个简单的发布后应用程序。这不好!如果他喜欢10公里,可能他的应用程序会变大。他会对一万人这样做吗?如果他在1页5篇文章中有50k字符串数组呢?这肯定会溢出。他需要使用云函数来创建一个字段,该字段跟踪服务器上字段的长度。然后进行查询,看看用户是否喜欢这篇文章。@AlexandruStroescu Firestore文档ID的大小是29个字节,计算为每个文档的UTF-8编码字节数+1。因此,如果您确实获得了50k字符串,那么在字符串数组中得到的字符串将少于1.5MB,因此这里不会出现溢出。然而,您认为这种方法是不可伸缩的,这是正确的。一种更具可伸缩性的方法是在POST下创建一个likedBy子集合,并用userId填充它,但这将更加昂贵,因为可伸缩性成本更高。然而,所提出的问题并不能保证构建FS的负担。这是可行的,但存在一些安全问题。他需要一些强有力的安全规则。
    Theme(
       data: ThemeData(splashColor: Colors.red[200]),
       child: Material(
          elevation: 0,
          shape: CircleBorder(),
          clipBehavior: Clip.hardEdge,
          color: Colors.transparent,
          child: InkWell(
             child: Padding(
                padding: const EdgeInsets.all(10),
                child: Icon(
                   Icons.favorite,
                   color: _isLiked ? Colors.red : Colors.black12,
                   size: 20,
                 ),
              ),
              onTap: () {
                 if(_isLiked){
                    setState(() {
                       _isLiked = false;
 //Here you need to update the backend status(if user dislike). This depends on your architecture.
                       _replyRateManager(widget.replyData.replyId,_currentUser,"DELETE_REPLY_RATE");
                       _rateCount -= 1;
                    });
                 }else{
                     setState(() {
                        _isLiked = true;
 //Here you need to update the backend status(if user like). This depends on your architecture.
                        _replyRateManager(widget.replyData.replyId,_currentUser,"ADD_REPLY_RATE");
                        _rateCount += 1;
                     });
                 }
              },
           ),
        ),
    )