我正在开发一个 flutter 类(class)应用程序。因此有一个子集合用于显示类(class)内的剧集。我在创建剧集时发送剧集编号,以使用 firebase orderBy
函数对剧集进行排序。
它可以工作,但无法正确订购。 (我所说的工作是指它的排序不同但困惑)。
这里是所有代码和详细信息
读取剧集代码
@override
Stream<List<EpisodesModel>> readEpisode({
required id,
}) {
return FirebaseFirestore.instance
.collection('courses')
.doc(id)
.collection('eps')
.orderBy('episodeNum', descending: false) // the eps num is the number that I send to firebase db when creating course
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => EpisodesModel.fromJson(doc.data()))
.toList());
}
但这就是值的排序方式
如果需要任何其他代码,请评论,
最佳答案
看起来 episodeNum
字段中的值存储为字符串值,而不是数字。并在 lexicographical ordering (Firestore 用于字符串值)“10”位于“2”之前,就像“aa”位于“b”之前。
要解决此问题,请将 episodeNum
值作为数字存储在数据库中,Firestore 会正确对它们进行排序。
关于flutter - Firebase 排序在 flutter 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72525466/