dart 如何从flutter中的firebase获取两个集合的数据?

km0tfn4u  于 2023-03-15  发布在  Flutter
关注(0)|答案(1)|浏览(113)

我正在做一个机械师和用户的应用程序,在这里:我把这个集合叫做'mecanicos'集合,但是我怎么称呼这两个集合呢?我是说用户集合和mecanicos集合
这是我的想法。

Future getDataFromFirestore() async {
await _firebaseFirestore
    .collection("mecanicos")
    .doc(_firebaseAuth.currentUser!.uid)
    .get()
    .then((DocumentSnapshot snapshot) {
  _userModel = UserModel(
    name: snapshot['name'],
    cedula: snapshot['cedula'],
    local: snapshot['local'],
    email: snapshot['email'],
    bio: snapshot['bio'],
    profilePic: snapshot['profilePic'],
    createdAt: snapshot['createdAt'],
    phoneNumber: snapshot['phoneNumber'],
    uid: snapshot['uid'],
  );
  _uid = userModel.uid;
});

}

vfwfrxfs

vfwfrxfs1#

您可以将文档的引用保存在两个单独的变量中,然后使用Future.wait来等待两个响应。一旦收到响应,您就可以使用“mecanicos”和“user”快照来进一步处理数据。希望这对您有所帮助。
代码:

Future getDataFromFirestore() async {
  final mecanicosReference = _firebaseFirestore
      .collection("mecanicos")
      .doc(_firebaseAuth.currentUser!.uid);
  final userReference = _firebaseFirestore
      .collection("users")
      .doc(_firebaseAuth.currentUser!.uid);

  final responses = await Future.wait([mecanicosReference.get(), userReference.get()])
  
  final mecanicosSnapshot = responses[0];
  final userResponse = responses[1];

  _userModel = UserModel(
      name: mecanicosSnapshot['name'],
      cedula: mecanicosSnapshot['cedula'],
      local: mecanicosSnapshot['local'],
      email: mecanicosSnapshot['email'],
      bio: mecanicosSnapshot['bio'],
      profilePic: mecanicosSnapshot['profilePic'],
      createdAt: mecanicosSnapshot['createdAt'],
      phoneNumber: mecanicosSnapshot['phoneNumber'],
      uid: mecanicosSnapshot['uid'],
    );
    _uid = userModel.uid;
}

相关问题