Flutter & Firebase:如何检查项目是否存在于 Firebase 文档中的数组中

Flutter & Firebase: How to check if an item exists inside of an array within a Firebase document

我正在尝试 waitUser(array) 检查存在的值我该怎么做?

//如果waitUser数组字段存在2returntext( "本文") else return 文本("其他文本")

      body: StreamBuilder(
    stream: FirebaseFirestore.instance
        .collection('events')
        .where('eventCity', isEqualTo: 'Ankara')
        .snapshots(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (!snapshot.hasData) {
        // ERROR
        return Center(
          child: CircularProgressIndicator(),
        );
      } else {
        return Container(
          child: ListView(
            scrollDirection: Axis.vertical,
            children: snapshot.data.docs.map((document) {
              // if waitUser array field exist 2 return text("this text") else return text("other text")
            }).toList(),
          ),
        );
      }
    },
  ),

在构建器的其他部分执行此操作:

return Container(
          child: ListView(
            scrollDirection: Axis.vertical,
            children: snapshot.data.docs.map((document) {
              // Get the waitUser array from the document.
              List<dynamic> waitUser = document.data()["waitUser"] as List<dynamic>;

              // Check for "2" in the array.
              if (waitUser.contains("2")) {
                // "2" was in the array.
                return text("this text");
              } else {
                // "2" was not in the array.
                return text("other text");
              }
            }).toList(),
          ),
        );

这将收集满足您 eventCity == "Ankara" 条件的每个文档。对于这些文档中的每一个,它将检查 waitUser 数组中的“2”。如果数组中存在“2”,则returntext("this text");否则,它将 return text("other text").