flutter firestore收集数据按日期排序

vulvrdjw  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(121)

我试图按日期排序来自firestore数据库的数据,这些数据以毫秒为单位,但当我使用.orderBy('date',declining:true)在我的代码中,应用程序中没有任何内容,当我删除它时,所有内容都消失了fine.it给了我一个空列表

class FirebaseManager {
  static bool existEmail = false;
  static bool weakPassword = false;
  static CollectionReference<TaskModel> getTasksCollection() {
    return FirebaseFirestore.instance
        .collection("Tasks")
        .withConverter<TaskModel>(
      fromFirestore: (snapshot, _) {
        return TaskModel.fromJson(snapshot.data()!);
      },
      toFirestore: (task, _) {
        return task.toJson();
      },
    );
  }
 static Stream<QuerySnapshot<TaskModel>> getTasks(DateTime date) {
    return getTasksCollection()
        .orderBy('date', descending: true)
        .where("userId", isEqualTo: FirebaseAuth.instance.currentUser?.uid)
        .where("date",
            isEqualTo: DateUtils.dateOnly(date).millisecondsSinceEpoch)
        .snapshots();
  }

}
StreamBuilder<QuerySnapshot<TaskModel>>(
            stream: FirebaseManager.getTasks(selectedDate),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const Center(child: CircularProgressIndicator());
              }
              if (snapshot.hasError) {
                const Center(child: Text("something went wrong"));
              }
              List<TaskModel> tasks =
                  snapshot.data?.docs.map((doc) => doc.data()).toList() ?? [];
              return ListView.builder(
                  itemBuilder: (context, index) {
                    return TasksItem(tasks[index]);
                  },

从firestore订购数据,它返回空数据。

7lrncoxx

7lrncoxx1#

当您向查询添加order-by子句时,听起来Firestore需要composite index,而您还没有定义该索引。
这听起来像你错过了必要的索引,你必须create yourself。您可以捕获Firestore为此抛出的错误并单击那里的链接,或者您可以转到Firestore控制台并从头开始创建索引,
另见:

相关问题