【发布时间】: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