【问题标题】:How to fetch sorted data from firebase in flutter如何在flutter中从firebase中获取排序数据
【发布时间】:2022-01-15 17:47:07
【问题描述】:

在这里,我想按照我上传笔记的顺序对笔记进行排序。 在这里,我使用了 orderBy('Time') 但这不起作用,而且我得到的数据不是按时间顺序排列的。 而且我不知道这个 orderBy 函数在这里是如何工作的。也请参考我一些文件。 提前致谢


 FutureBuilder<QuerySnapshot>(
             ***future:ref.orderBy('Time').get(),***
                builder: (context,snapshot){
                if(snapshot.hasData){
                  return ListView.builder(
                    physics: ClampingScrollPhysics(),
                    padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                       shrinkWrap: true,
                      itemCount: snapshot.data!.docs.length,
                      itemBuilder: (context,index){
                      Map data= snapshot.data!.docs[index].data() as Map;
                      DateTime mynote_time=data['Time'].toDate();
                      String formattedTime =
                      DateFormat.yMMMd().add_jm().format(mynote_time);
                      return Column(
                        children: [
                          Card(
                            child: Column(
                              children: [
                                Padding(
                                  padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
                                  child: Text(
                                      "${data['title']}",
                                    style: TextStyle(
                                      fontWeight: FontWeight.w700,
                                      fontSize: 20,
                                    ),
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
                                  child: Text(
                                    "${data['description']}",
                                    style: GoogleFonts.lato(
                                      fontSize: 14,
                                    ),
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.bottomRight,
                                  child: Text(
                                    formattedTime
                                  ),
                                )
                              ],
                            ),
                            borderOnForeground: true,
                            shadowColor: Colors.blueAccent,
                            color: Colors.grey[100],
                            elevation: 10,
                          ),
                        ],
                      );
                      }
                      );
                }
                else if(snapshot==null){
                  return Text('Please Enter some notes');
                }
                else return Padding(
                  padding: EdgeInsets.fromLTRB(150, 200, 0, 50),
                  child: SpinKitPumpingHeart(
                    color: Colors.blueAccent,
                    size: 100,
                    duration: Duration(seconds: 2),
                  ),
                );
                }),

下面的代码是将我的数据发送到 firebase 的函数。

void addnote() async{
    CollectionReference ref= FirebaseFirestore.instance
        .collection("users")
        .doc(FirebaseAuth.instance.currentUser!.uid)
        .collection('Notes');

    var data={
      'title': titletext.text,
      'description': destext.text,
      'Time': DateTime.now(),
    };
    ref.add(data);

  }

【问题讨论】:

  • 我会像这样添加时间:" 'Time': FieldValue.serverTimestamp()" 和 "DateTime mynote_time=((data['Time']?? Timestamp.now()) 作为 Timestamp )。迄今为止();”但我不确定。

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

firestore 为其提供了一个名为 orderBy 的函数。它应该是这样的:

Firestore.instance
     .collection("users")
     .doc(FirebaseAuth.instance.currentUser!.uid)
     .collection('Notes')
     .orderBy('Time', descending: true or false).get();

这个函数你可以传递给你的 Futurebuilder

【讨论】:

  • 默认情况下它是假的......我会说它已经订购了。这只是为了反向。
猜你喜欢
  • 2019-02-07
  • 1970-01-01
  • 2021-04-08
  • 2021-09-08
  • 2020-07-21
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多