【问题标题】:Query data on firebase realtime实时查询firebase数据
【发布时间】:2020-06-09 17:46:37
【问题描述】:

我有基于 userId 过滤数据的颤振代码。但我也希望根据基于日期的降序对数据进行排序。就像我想先显示当天的数据,然后再显示前一天的数据。在firestore中它是可行的,而我发现它很难实时处理。下面是代码 sn-p,我在其中添加了查询以按 userId 过滤。下面我也想添加基于日期的排序数据(我正在保存时间戳)

_database.reference().child("node name here..").orderByChild("userId").
equalTo("my user id here...").once().then((snapshot )async{}

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database


    【解决方案1】:

    FirebaseDatabase 首先返回最旧的数据,为了查看最新数据,我建议您反转列表或同时将时间戳 * 保存 -1 以使文档“最旧”

    例子

    int time = DateTime.now().microsecondsSinceEpoch; var timestamp = time * -1;

    您可以按照此示例查询您的数据

         return StreamBuilder(
            stream: FirebaseDatabase.instance
                .reference()
                .child('node')
                .orderByChild('userid')
                .equalTo(userId)
                .onValue,
            builder: (BuildContext context, AsyncSnapshot<Event> snapshot) {
              if (snapshot.hasData) {
                if (snapshot.data.snapshot.value != null) {
                  Map<dynamic, dynamic> map = snapshot.data.snapshot.value;
                  List<dynamic> list = map.values.toList();
    
    
                  return ListView.builder(
                    itemCount: list.length,
                    padding: EdgeInsets.all(4.0),
                    itemBuilder: (BuildContext context, int index) {
                      return Column(
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(13),
                            child: Container(
                              decoration:
                                  BoxDecoration(color: Colors.blueGrey[100]),
                              child: ListTile(
                                title: Text(list[index]["title"]),
                                subtitle: Text(list[index]["text"]),
    ),
    

    PS

    观察你的控制台,看看你是否获得了创建index的链接

    【讨论】:

    • 出于某种奇怪的原因,实时以无序的方式返回给我数据。但是在节点中,我可以在节点的最后看到最新数据。虽然我从 firebase 的多个用户列表中仅获取特定用户 ID 的数据,但它以无序的方式返回我。如果我要扭转它,它不会解决我的问题。
    • 我应该在这里做什么。
    • Rtdb 首先返回最旧的数据..尝试使用负时间戳
    • 好的,我将在时间戳中添加-1,同时在节点中添加数据,但是在代码中的哪里添加查询时间戳?
    • 不确定您真正想要什么,但如果要使用时间戳查询,您可以随时添加.orderByChild('userid')
    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多