【问题标题】:Flutter - Getting data from firestore in a loopFlutter - 循环从firestore获取数据
【发布时间】:2018-12-22 02:35:12
【问题描述】:

所以基本上我有一个用户集合,并且在每个用户中都有一个用于该用户的待处理好友请求的子集合,类似于:

/users/[auto-id]/friend_requests/[auto-id]/{user: ref to another user}

但一个用户显然可以同时有多个请求,我很难正确获取数据。

我正确尝试做的是获取子集合“friend_requests”中的用户列表:

_loadFriendRequests() async {
    try {
      this._users.addAll(await _dataService.fetchFriend());
    } catch (e, stackTrace) {
      printException(e, stackTrace, "Error loading friends");
    }
}

在数据服务中:

Future<List<User>> fetchFriend() async {
    final querySnapshot =
        await _currentUserDoc.reference.collection("friend_requests").getDocuments();

    return await Future.wait(querySnapshot.documents.map((doc) async {
      final user = await doc["user"].get();
      User(
        id: user["id"],
        name: user["name"],
      );
    }).toList(growable: false));
  }

这只是我尝试的最后一件事,但我尝试了很多方式,Future.wait() 也有,有/没有 async/await 等 我似乎无法让它工作......

【问题讨论】:

标签: firebase asynchronous dart google-cloud-firestore flutter


【解决方案1】:

您在final user = await doc["user"].get(); 之后缺少返回语句



如果你使用Streams 或花哨的rxdart,这会变得容易得多

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 2023-03-15
    • 2021-04-17
    • 2022-09-23
    • 2022-06-24
    • 1970-01-01
    • 2018-12-20
    • 2021-10-13
    相关资源
    最近更新 更多