首页 > 解决方案 > 子集合是创建玩家“好友列表”的最佳方式吗?

问题描述

我很难处理子集合(这是我的第一个颤振/firebase/nosql 项目)

我想在玩家集合中实现一个“朋友列表”,并且我需要一个与朋友列表中其他玩家的子集合,这是最好的方法吗?如果是,我如何将子集合添加到文档中?

final CollectionReference playerCollection =
      Firestore.instance.collection('player');

Future updatePlayerData(
    String nickname,
    int gamesWon,
    int points,
  ) async {
    return await playerCollection.document(uid).setData({
      "nickname": nickname,
      "gamesWon": gamesWon,
      "points": points,
    });
  }

标签: firebasefluttergoogle-cloud-firestore

解决方案


你可以像这样在文档中创建subCollection ,

final CollectionReference playerCollection =
Firestore.instance.collection('player');

Future updatePlayerData(
    String nickname,
    int gamesWon,
    int points,
  ) async {
    return await playerCollection.document(uid).collection(frdUid).document(frdUid).setData({    
//frdUid is unique id
      "nickname": nickname,
      "gamesWon": gamesWon,
      "points": points,
    });
  } 

推荐阅读