【问题标题】:How to save list of user Ids in a local list in Flutter?如何在 Flutter 的本地列表中保存用户 ID 列表?
【发布时间】:2021-05-19 00:26:15
【问题描述】:

我在 Firestore 中有一个集合,我附上下面的结构 -

'following' 集合是由用户 ID 集合组成的主集合,每个用户 ID 都有 'usersFollowing' 集合,其中包含所有用户 ID当前用户正在关注。

我想要的是当用户登录时将'userFollowing'集合的所有用户ID保存到本地列表中,我该怎么做?

我尝试过但无法检索数据,我在下面附上了我的代码,

QuerySnapshot snapshot = await FirebaseFirestore.instance.collection('following')
        .doc(widget.currentUser.id)
        .collection('userFollowing').get();

List<QueryDocumentSnapshot> docs = snapshot.docs;
List<FollowersId> ids = docs.map((doc) => FollowersId.fromDocument(doc)).toList();

FollowersId 模型

class FollowersId {
  
  String userFollowerId;
  FollowersId({userFollowerId});

  factory FollowersId.fromDocument(doc) {
    return FollowersId(
      userFollowerId: doc.data(),
    );
  }
}

【问题讨论】:

  • 您可以将其保存到本地数据库,sqflite 或类似的。
  • 我已经在firestore中有数据库模型,唯一的事情就是在这种情况下检索userFollowing id的信息。

标签: flutter google-cloud-firestore flutter-layout


【解决方案1】:

要获取数据,您可以这样做:-

List<String> ids=[];
  QuerySnapshot snapshot=await FirebaseFirestore.instance.collection('following')
    .document(widget.currentUser.id).collection("userFollowing").getDocuments();
  snapshot.documents.forEach((doc) { 
    ids.add(doc.documentID);
  });

执行上述代码后,所有 id 将存储在列表 id 中。 如果您想将其本地存储到您的设备,那么您可以使用this

【讨论】:

  • doc.documentID 是什么意思?我的意思是documentID是什么,表中没有这样的变量。
  • doc.Document id 表示您将获得该文档的文档 id。就像你的情况一样,它是 113000 等等
  • 没有doc.documentID这个函数,不知道有没有被弃用。如果可能的话,你能给我任何书面文件吗?
  • this 中,您可以看到它在那里,但如果它被贬低,您可以在创建文档时将文档 ID 作为字段存储在文档中。
猜你喜欢
  • 2021-06-10
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
相关资源
最近更新 更多