【问题标题】:FirebaseAnimatedList query when query don't have data to fetch当查询没有要获取的数据时,FirebaseAnimatedList 查询
【发布时间】:2020-06-19 23:29:22
【问题描述】:

我是 Flutter 和 firebase 的超级新手,我尝试使用下面示例代码中的 FirebaseAnimatedList 从 firebase 实时数据库中查询一些数据,我尝试传递 this.userid 以根据传递的值获取数据,但有时是数据库根据传递的内容,可能没有数据。我只想知道如何处理此类情况并显示反馈说没有与查询匹配的数据。它基本上是一种搜索,它可能没有用户寻找的值。

示例代码:


    FirebaseAnimatedList(
            query: ref.child("users").orderByChild("id").equalTo(this.userid),
            itemBuilder: (BuildContext context, DataSnapshot snapshot,
                Animation<double> animation, int index) {
              return SizeTransition(
                sizeFactor: animation,
                axis: Axis.horizontal,
                axisAlignment: -0.8,
                child: Column(
                  textDirection: TextDirection.ltr,
                  verticalDirection: VerticalDirection.down,
                  children: <Widget>[
                    SizedBox(
                      height: 100.0,
                    ),
                    Text(
                      "User ID : " + snapshot.value["id"],
                    ),
                    SizedBox(
                      height: 50.0,
                    ),
                  ],
                ),
              );
            }));

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database


    【解决方案1】:

    snapshot 的类型为DataSnapshot,因此您可以使用属性exists 来检查数据是否存在:

    itemBuilder: (BuildContext context, DataSnapshot snapshot,
                    Animation<double> animation, int index) {
                if(!snapshot.exists){
                      return Text("No Data");
                } 
                else {
                  return SizeTransition(
                    sizeFactor: animation,
                    axis: Axis.horizontal,
                    axisAlignment: -0.8,
                    child: Column(
                      textDirection: TextDirection.ltr,
                      verticalDirection: VerticalDirection.down,
                      children: <Widget>[
                        SizedBox(
                          height: 100.0,
                        ),
                        Text(
                          "User ID : " + snapshot.value["id"],
                        ),
                        SizedBox(
                          height: 50.0,
                        ),
                      ],
                    ),
                  );
                 } 
                }
    

    查看以下内容了解更多信息:

    https://pub.dev/documentation/firebase_dart/latest/firebase_dart/DataSnapshot-class.html

    【讨论】:

    • 我刚刚检查过先生它不起作用无法访问 Datasnapshot 上的 .exist 方法
    • 存在带 s 的属性
    • 非常感谢您的回复。抱歉打错了是的,我尝试使用 .exists 但它不起作用存在不是 Datasnapshot 的可用方法。
    • 试试if(snapshot.value == null){return Text("No Data") ;}
    • 我认为如果查询没有返回任何值 FirebaseAnimatedList 不要继续itembuilder:先生
    【解决方案2】:
    Query q=ref.child("users").orderByChild("id").equalTo(this.userid);
    q==null?Center(child: CircularProgressIndicator()): FirebaseAnimatedList(
      query: ref.child("users").orderByChild("id").equalTo(this.userid),
      itemBuilder: (BuildContext context, DataSnapshot snapshot,
        Animation<double> animation, int index) {
      return SizeTransition(
        sizeFactor: animation,
        axis: Axis.horizontal,
        axisAlignment: -0.8,
        child: Column(
          textDirection: TextDirection.ltr,
          verticalDirection: VerticalDirection.down,
          children: <Widget>[
            SizedBox(
              height: 100.0,
            ),
            Text(
              "User ID : " + snapshot.value["id"],
            ),
            SizedBox(
              height: 50.0,
            ),
          ],
        ),
      );
    }));
    

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 1970-01-01
      • 2021-03-04
      • 2011-11-01
      • 2015-11-21
      • 2018-09-27
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多