【问题标题】:flutter firestore contains any query not fetching documentsflutter firestore 包含任何不获取文档的查询
【发布时间】:2021-08-18 08:59:59
【问题描述】:

我在 firebase 的播放器下存储了一些文档,它们都有一个 id 字段,其中包含 num 值(例如 1、3 或 25)。现在我只有 30 名玩家,但是当我只需要一些来避免不必要的阅读时,我会尽量避免获得所有 30 名玩家。我有一个包含我想要的所有玩家 ID 的列表,我正在尝试使用 arrayContainsAny 来解决我的问题,例如:

FirebaseFirestore.instance.collection('players').where("id", arrayContainsAny: widget.selectedPlayer);

正如我所说,widget selected player 是列表,我也尝试使用 [1, 2] 代替 widget.selectedPlayer 但它们都没有返回任何文档。

PS:我尝试使用等于,它工作正常:

FirebaseFirestore.instance.collection('players').where("id", isEqualTo:  27)

这是我的部分代码

players = FirebaseFirestore.instance.collection('players').where("id", arrayContainsAny: widget.selectedPlayer);


return StreamBuilder<QuerySnapshot>(
  stream: players.snapshots(),
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (snapshot.hasError) {
      return Text('Something went wrong');
    }

    if (snapshot.connectionState == ConnectionState.waiting) {
      return Text("Loading");
    }
    var sortedPlayers = sortPlayersByPosition(snapshot.data.docs);
    debugPrint("player docs: " + sortedPlayers.toString());
    return ListView.builder(
      padding: EdgeInsets.all(0.0),
      itemCount: snapshot.data.docs.length + 1,
      itemBuilder: (BuildContext context, int index) {
        if (index == 0) {
          // return the header
          return playerHeaderRow(header);
        }
        index -= 1;
        if (widget.selectedPlayer.contains(sortedPlayers[index]["id"])) {
          return buildPlayers(sortedPlayers[index]);
        } else {
          // need to return a empty widget here but return null stop the loop when we come to a false case
          // this will create a empty widget : https://stackoverflow.com/a/55796929
          return SizedBox.shrink();
        }
      },
    );
  },
);

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    好的,如果你想使用这个功能"An in query returns documents where the given field matches any of the comparison values."

    你应该使用 where 而不是 arraycontainsany,我希望他们在某个地方有一个文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-21
      • 2018-10-20
      • 1970-01-01
      • 2021-07-20
      • 2021-07-06
      • 1970-01-01
      • 2020-10-11
      • 2019-05-05
      相关资源
      最近更新 更多