【问题标题】:Flutter: retrieve log history from real time databaseFlutter:从实时数据库中检索日志历史记录
【发布时间】:2022-01-16 12:31:14
【问题描述】:

更新

我想从路径 Staff=>randomID1=>log=>randomID2 实时检索用户日志历史记录 我想在 showDialog 中显示来自 randomID2 的数据。

我的路径:final databaseRef = FirebaseDatabase.instance.reference().child('Staff');

       child: FirebaseAnimatedList(
          query: dataRef,
          itemBuilder: (BuildContext context, DataSnapshot snapshot,
              Animation<double> animation, int index) {

            var log = snapshot.value['log'];
            var dates = log.keys.toList();
            var key = dates[index];

            return Card(
              child: ListTile(
                title: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('Email             :  ${snapshot.value['email']}'),
                    Text(
                        'Mail Status   :  ${snapshot.value['mail-status'].toString()}'),
                    Text('Date               :  ${snapshot.value['date']}'),
                    Text('Time              :  ${snapshot.value['time']}'),

                    Text(snapshot.value['log'][key]['current-date']),
                    Card(
                      child: ListTile(
                        subtitle: Column(
                          children: <Widget>[
                            for(var i = 0; i<log.length; i++)...[
                              Text(snapshot.value['log'][key]['current-date'])
                            ],       
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            );
          })),

我的错误:

Exception has occurred. NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: []("current-date"))

我不知道为什么它显示相同的键值

【问题讨论】:

    标签: flutter firebase-realtime-database


    【解决方案1】:

    问题出在这一行:

    Text(snapshot.value['log'][index]['current-date'])
    

    index 将是一个连续的数值(0、1、2、3 等),而您显示的 JSON 中的键是格式化的日期。所以snapshot.value['log'][index]在JSON中不存在并返回null,这解释了你得到的错误信息。


    要在 JSON 中的索引处获取键,您可以这样做:

    var log = snapshot.value['log']
    var dates = log.keys.toList();
    var key = dates[index];
    

    注意:您可能需要转换或转换此 sn-p 中的某些类型。如果您遇到该类型的错误,请尝试在评论前通过搜索错误消息来解决它们。

    然后你可以使用key in:

    Text(snapshot.value['log'][key]['current-date'])
    

    【讨论】:

    • 如果我想在 log child 中显示所有数据列表怎么办?因为我有很多基于当前时间和日期的数据。
    • 我不确定我理解你的意思。您是否尝试过我回答中的代码/方法?当你这样做时,你得到了什么?
    • 我做到了。它仅显示一个数据,而不是子日志中的所有数据列表。路径“日志”中的示例。我有数据列表 20220110095744:{date:2022-01-10}、20230110095733{date:2022-09-22}。我想在列表中显示 20220110095744 和 20230110095733 的日期。
    • 您确定您使用的是从itemBuilder 获得的index 吗?如果是这样,您可以编辑您的问题并使用我的答案中您如何实施解决方案的代码添加更新吗?
    • 我已经更新了我的帖子。你能检查一下吗..如果继续问同样的问题,我很抱歉T.T
    猜你喜欢
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2021-02-06
    相关资源
    最近更新 更多