简体   繁体   English

Firestore 如何在 Flutter 中获取具有特定用户 ID 的特定数据

[英]Firestore how to fetch specific data with specific user id in Flutter

I have stream builder, and I fetch all the users.我有 stream 生成器,我获取了所有用户。 After that, using bloc (or any state management) I filter them.之后,使用 bloc(或任何 state 管理)过滤它们。 After filtering, I create a Set which has filtered user ids (I mean there is a set, and it has user ids).过滤后,我创建了一个已过滤用户 ID 的集合(我的意思是有一个集合,它有用户 ID)。

Now, using with these uids I want to fetch filtered user datas.现在,使用这些 uid,我想获取过滤后的用户数据。 I did with FirebaseFirestore.instance.collection(...).doc(userId).get() , after that it gives Future<String?> .我用FirebaseFirestore.instance.collection(...).doc(userId).get()做了,之后它给出了Future<String?> What should I do?我应该怎么办?

here is the codes:这是代码:

class HomePageBody extends StatelessWidget {
  HomePageBody({
    Key? key,
    required this.mapsState,
  }) : super(key: key);

  final MapsState mapsState;

  final Set users = {};
  @override
  Widget build(BuildContext context) {

    return StreamBuilder<QuerySnapshot>(
      stream: firestoreStream,
      builder: (context, AsyncSnapshot snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting || snapshot.connectionState == ConnectionState.none) {
          return const CustomProgressIndicator(
            progressIndicatorColor: blackColor,
          );
        } else if (!snapshot.hasData) {
          return const CustomProgressIndicator(
            progressIndicatorColor: blackColor,
          );
        } else if (snapshot.hasData) {
          final usersDatas = snapshot.data.docs;

          for (var userDatas in usersDatas) {
            if (userDatas["latitude"] == null || userDatas["longitude"] == null) {
            } else {
              users.add(userDatas);
            }
          }
          context.read<MapsCubit>().filterUsersWithRespectToDistance(users: users);
          final usersWithInTenKilometers = mapsState.usersWithInTenKilometers;

          **// HERE WE HAVE FILTERED USERS, AND THIS SET HAS USER IDS.**

          return ListView.builder(
                  padding: const EdgeInsets.only(top: 75),
                  itemCount: usersWithInTenKilometers.length,
                  itemBuilder: (context, index) {
                    final userId = usersWithInTenKilometers.elementAt(index);
                    final usersDatas = FirebaseFirestore.instance
                        .collection("users")
                        .doc(userId)
                        .get();
                        // I did like this, but it does not work.

                    return CustomListTile(
                      userImageUrl: "https://picsum.photos/200/300",
                      userStatus: "userStatus",
                      userName: "userName",
                    );
                  },
                );
        }
        return const CustomProgressIndicator(
          progressIndicatorColor: blackColor,
        );
      },
    );
  }
}

Consequently, I have a Set (or you can think like List), and it has user ids.因此,我有一个 Set(或者您可以像 List 一样思考),并且它有用户 ID。 Using these user ids, fetch user datas basically from the Firestore (email: ..., password: ... etc)使用这些用户 ID,基本上从 Firestore 获取用户数据(电子邮件:...,密码:...等)

  final userId = usersWithInTenKilometers.elementAt(index);
                    final users = FirebaseFirestore.instance
                        .collection("users")
                        .doc(userId)
                        .get()
                        .then((value) => value)
                        .then((value) => value.data());

                    return FutureBuilder(
                      future: users,
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          final convertUserDataToMap =
                              Map<String, dynamic>.from(snapshot.data as Map<dynamic, dynamic>);
                          final List userDataList = convertUserDataToMap.values.toList();
                          final userId = userDataList[0];
                          final userLong = userDataList[1];

.... ....

I solved like this我这样解决

Since you get back a Future<String?> , I'd typically first consider using a FutureBuilder to render that value.由于您返回Future<String?> ,我通常会首先考虑使用FutureBuilder来呈现该值。

If you have multiple values that each is loaded asynchronously separately (like is the case here with your multiple get() calls), I'd start with using a separate FutureBuilder for each Future .如果您有多个值,每个值都是单独异步加载的(就像您多次调用get()时的情况),我将从为每个Future使用单独的FutureBuilder开始。 Only if I'd run into practical problems with that, would I start considering more complex options, such as Future.wait() to wait for all of them to complete before rendering any result.只有当我遇到实际问题时,我才会开始考虑更复杂的选项,例如Future.wait()以等待所有选项完成后再呈现任何结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过特定用户获取 firestore 中的数据 - how to get data in firestore by specific user 如何使用特定参数 flutter 从 Firestore 中删除数据? - How to delete data from Firestore with specific parameter flutter? 如何使用 flutter 和 dart 从 Cloud Firestore 检索特定数据? - how to retrieve specific data from Cloud Firestore using flutter and dart? 如何从 Firestore 获取特定数据并在 Objective-C 中更新它 - How to fetch specific data from Firestore and update it in Objective-C 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website 如何从 flutter 中的云 firestore 获取数据? - How to fetch data from cloud firestore in flutter? Flutter firestore 使用特定键写入数据 - Flutter firestore write data with specific key 当我使用 Flutter Firestore 进行身份验证流程(登录、注销)时,如何为特定用户获取正确的数据? - How I can get the right data for specific user when I did auth flow (log in, log out) with Flutter Firestore? 如何从 flutter 中 user.uid 等于文档 ID 的 Cloud firestore 获取数据? - how to get data from cloud firestore where user.uid equal to document id in flutter? Flutter & Firestore:当Firestore无法获取特定字段时,如何安全获取数据? - Flutter & Firestore: What way to get data with safety when the Firestore can't get a specific field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM