首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将firebase中的字段与flutter进行比较?

问如何将firebase中的字段与flutter进行比较?
EN

Stack Overflow用户
提问于 2021-04-28 02:47:38
回答 1查看 264关注 0票数 1

我正在构建一个聊天应用程序,我正在尝试构建一个屏幕,其中包含与其他用户的用户历史对话。因此,基本上我要做的是获取消息字段映射中的所有值(仅取值,而不是键),如下所示:

我想将它与chatRoom中的文档进行比较,如下所示:

​

如果我比较它们,结果是真的,我想向用户显示该chatRoom中的数据。我真的很难理解如何比较他们提前感谢。

编辑:代码如下:

代码语言:javascript
复制
  final String chatId;
  Chat({this.chatId});
  @override
  _ChatState createState() => _ChatState();
}

class _ChatState extends State<Chat> {
  // Our own id
  final String currentUserId = auth.currentUser?.uid;

  // Loading state
  bool isLoading = false;

  // Query variable
  Future<QuerySnapshot> chatHistoryResults;

  // All the user rooms will be in this variable
  List<String> roomsUserList = [];

  // All the chat rooms will be in this variable
  List<String> roomsChatList = [];

  // All the history chat tickets will be here.
  List<QuerySnapshot> tickets = [];

  // Getting all the current user rooms in a list of Id rooms.
  //TODO: fix the chat history
  getRooms() async {
    roomsUserList = [];
    //roomsChatList = [];
    DocumentSnapshot myDocUser = await userRef.doc(currentUserId).get();
    Map<dynamic, dynamic> userRoomsMap = await myDocUser.get('messages');
    userRoomsMap.forEach((key, value) {
      roomsUserList.add(value);
    });
    print(roomsUserList);
    roomsUserList.forEach((element) async {
      String chatDoc = chatRef.doc(element).get().toString();
      roomsChatList.add(chatDoc.toString());
    });
    print(roomsChatList);
  }

  // Build the story tickets here
  buildStoryTickets() {
    return FutureBuilder(
      future: chatRef.get().then((snapshot) {
        snapshot.docs.forEach((element) {});
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Provider<AuthService>(
      create: (context) => AuthService(),
      child: Scaffold(
        backgroundColor: Colors.grey[350],
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(55.0),
          child: Header(
            title: 'Chat',
          ),
        ),
        body: ListView(
          children: [
            //buildStoryTickets(),
            Container(
              height: 100,
              width: 100,
              child: FlatButton(onPressed: () {
                getRooms(); // Just to check 
              }),
            ),
          ],
        ),
      ),
    );
  }
}

我设法将"messages“映射中的所有值插入到一个名为"roomUserList”的列表中

EN

回答 1

Stack Overflow用户

发布于 2021-04-28 04:00:11

首先,您可以使用StreamBuilder从用户和聊天室获取所有数据。一旦你有了消息和聊天室,你就可以通过开发代码逻辑来比较它们。

从firestore获取数据的PFB示例代码:

代码语言:javascript
复制
 StreamBuilder<QuerySnapshot>(
              stream: _firestore.collection('users').snapshots(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  //add your comparison logic here
                  );
                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67288983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档