0

img_firebase

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

// 如果waitUser 数组字段存在2 return text("this text") else return text("other text")

      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(),
          ),
        );
      }
    },
  ),
4

1 回答 1

1

在构建器的 else 部分中执行以下操作:

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”,则返回text("this text");否则,它将返回text("other text").

于 2021-05-08T22:44:52.663 回答