【问题标题】:Listview builder only different valuesListview builder只有不同的值
【发布时间】:2022-01-06 08:00:15
【问题描述】:

我在 Stream Builder 中有一个列表视图构建器,内容是在快照中获取的,但是,我遇到的问题是我只需要显示一次重复的数据。

如您所见,同一个人查看了列表并向我发送了一些聊天,我如何告诉 Listview Builder Return,仅在此示例中的人员和操作不同时才创建小部件,而不是 6 Eduardo Alas 查看您的列表和 4 Eduardo Alas 想给您发送消息,我只想显示 2 行,因为这是重复操作。

这是我的代码:

StreamBuilder(
                    stream: DatabaseMethods().getNotifications(),
                    builder: (context, snapshot) {
                      int lastdoc = 0;
                      if (snapshot.hasData) {
                        lastdoc = snapshot.data.docs.length - 1;
                      }
                      return snapshot.hasData
                          ? Expanded(
                              child: Container(
                                color: Color(0xFFF9F7F0),
                                child: ListView.builder(
                                    shrinkWrap: true,
                                    padding: const EdgeInsets.all(10),
                                    itemCount: snapshot.data.docs.length,
                                    reverse: false,
                                    itemBuilder: (context, index) {

                                      DocumentSnapshot ds =
                                          snapshot.data.docs[index];

                                      return NotificationInfo(
                                        id: ds.id,
                                        email: ds['email'],
                                        image: ds['image'],
                                        name: ds['name'],
                                        phone: ds['phone'],
                                        platform: ds['platform'],
                                        token: ds['token'],
                                        typeNot: ds['notification'],
                                        username: ds['username'],
                                        listingId: ds['listingId'],
                                        listingImage: ds['listingImage'],
                                        notificationId: ds.id,
                                        blockDemo: false,
                                        islast: index == lastdoc,
                                      );
                                      //}
                                    }),
                              ),
                            )
                          : Center(
                              child: RoofLoadingBar(
                                height: 100,
                              ),
                            );
                    },
                  )

有什么想法吗?

【问题讨论】:

    标签: flutter listview stream-builder


    【解决方案1】:

    您可以做的一件事是避免保存多个实例以供查看列表或想向您发送消息时使用,

    在发送消息的情况下,你可以使用userid作为doc id,这意味着你只能有一个来自用户的消息请求。当写入firebase时,你将解析一个docId并使用create_or_update .set 写你的数据,例如:

    FirebaseFirestore.instance
              .collection("notifications")
              .doc(userid)
              .set({"mymap":"data as usual"});
    

    在listing的情况下可以连接useridlistingId,使用.set保存数据,可以有一个counter字段。

    String? myDocId= userid+listingId;
    FirebaseFirestore.instance
                  .collection("notifications")
                  .doc(myDocId)
                  .set({"mymap":"data as usual"});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2021-12-30
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多