【问题标题】:How do i create a timeline feed using firebase in flutter?如何在 Flutter 中使用 Firebase 创建时间线提要?
【发布时间】:2020-06-26 08:41:53
【问题描述】:
- posts
    - userID
    - userID
          - randomID
          - randomID
- followers
    - personB userid
          - personA userid
- following 
    - personA userid
          - personB userid

这就是我的 firebase 集合的组织方式。

我如何为当前登录用户创建时间线供稿,其中时间线帖子应仅来自登录用户的以下列表? 注意:我不想使用 firebase 功能。CRUD 可以吗? 到目前为止我尝试了什么。

 getFollowing() async {
    QuerySnapshot snapshot = await followersRef
        .document(currentUser.id)
        .collection('userFollowing')
        .getDocuments();
    setState(() {
      followingList = snapshot.documents.map((doc) => doc.documentID).toList();
      print(followingList);
    });
  }
  getTimeline()async{
    QuerySnapshot snapshot = await postsRef
        .document(followingList)//getting an error here saying list<string> csn't be assigned to string.
        .collection('userPosts')
        .orderBy('timestamp', descending: true)
        .getDocuments();
    List<Post> posts =
    snapshot.documents.map((doc) => Post.fromDocument(doc)).toList();
    setState(() {
      this.posts = posts;
    });
  }

【问题讨论】:

  • 你能分享一下代码,我会编辑并发送
  • @Niteesh 我不知道如何连接两个集合(以下和帖子)。如果您给我一个想法,请尝试使用代码并回复您。我应该复制用户的帖子并将其保存到另一个集合中以供时间轴供稿使用?这可能吗?
  • 先获取以下列表的用户ID,然后使用用户ID获取每个用户的帖子
  • 我试过了,但我得到一个错误,说 list 不能分配给字符串。我已经编辑了我的问题并输入了我试过的代码。
  • 问题是文档只接受字符串参数而不接受列表

标签: firebase flutter dart


【解决方案1】:

我认为有效的方法是为每个帖子使用一个集合“帖子”和文档,在帖子文档中使用一个数组作为字段。

这意味着当用户添加帖子时,您必须将用户的关注者作为字段(列表

- posts(Collection)

    - post(Document)
 
         - followers(List<dynamic>)

你可以查询用户喜欢的帖子

Firestore.instance.collection('posts').where('followers', arrayContains: currentUserId).snapshots();
//this brings all queries with the tagged users as followers aka the users that follow the poster will be able to query this post.

如果你想更新你可以使用的数组

FieldValue.arrayRemove 或 FieldValue.arrayUnion,

编辑:作为最后的警告,我必须提到一个文档的数据量是有限制的,根据您的应用程序的规模,您必须决定是否放置一个不断增长的列表(例如作为关注者)在一个文档中为不断增长的列表创建另一个集合并为每个元素提供一个文档,因为文档不能被限制(这种方法可能会花费更多)。 p>

【讨论】:

    【解决方案2】:
    List timelinePosts =[];
    
    
    getFollowing() async {
        QuerySnapshot snapshot = await followersRef
            .document(currentUser.id)
            .collection('userFollowing')
            .getDocuments();
        setState(() {
          followingList = snapshot.documents.map((doc) => doc.documentID).toList();
          print(followingList);
        });
      }
    
    getTimeline()async{
    
    List posts = [];
       for( int i=0; i< followingList.length; i++)
    {
        QuerySnapshot snapshot = await postsRef
            .collections('posts/${followingList[i]}/userPosts')
            .orderBy('timestamp', descending: true)
            .getDocuments();
        posts+= snapshot.data.documents.map((doc) => {'id':doc.documentID,...doc.data}).toList();
    }
        setState(() {
          timelinePosts = timeLinePosts + posts;
        });
      }
    
    
    

    然后你可以在timelinePosts上使用Listview.builder(), 然而,这不是最有效的方法,因为您一次获取所有帖子。您还可以使用 Listview 控制器属性在滚动列表时实现获取帖子

    【讨论】:

    • @August 很高兴为您提供帮助
    • 你在 Flutter 的 dicord 服务器上吗?
    • 我们希望有像您这样的人。 discord.gg/8ct9ef
    • posts= snapshot.data.documents.map((doc) =&gt; {'id':doc.documentID,...doc.data}).toList();can't be assigned to quesysnapshot and timelinePosts returned null .Anyway我改变了整个代码,然后让它工作。你加入了discord服务器吗?
    • 你的用户名是什么?我想我还没有看到那里活跃
    猜你喜欢
    • 2021-10-08
    • 2023-01-25
    • 2018-11-26
    • 2021-08-16
    • 2015-07-14
    • 1970-01-01
    • 2019-12-12
    • 2017-08-29
    • 2021-10-27
    相关资源
    最近更新 更多