flutter - Firebase 排序在 flutter 中无法正常工作

标签 flutter firebase dart google-cloud-firestore

我正在开发一个 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());
  }

但这就是值的排序方式

enter image description here

如果需要任何其他代码,请评论,

最佳答案

看起来 episodeNum 字段中的值存储为字符串值,而不是数字。并在 lexicographical ordering (Firestore 用于字符串值)“10”位于“2”之前,就像“aa”位于“b”之前。

要解决此问题,请将 episodeNum 值作为数字存储在数据库中,Firestore 会正确对它们进行排序。

关于flutter - Firebase 排序在 flutter 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72525466/

相关文章:

json - 具有未知 key 的 JSON 序列化

Flutter 升级失败

dart - 在 Flutter Dart 中为列表中的每个项目创建小部件

javascript - Firebase 数据未保存到数据库

ios - 如何判断图片中是否没有文字--MLKit VisionCloudTextRecognizer

ios - 确保 Firebase 中的异步操作已完成

flutter - 使用 Flutter new integration_test 进行示例集成测试?

dart - Flutter - 列中的空间均匀不按预期工作

dart - 如何清除 Flutter 的图片缓存?

android - 将 Stack Widget 的内容转换为图像