【问题标题】:Flutter FirebaseAnimatedList query is not working properlyFlutter FirebaseAnimatedList 查询无法正常工作
【发布时间】:2020-06-20 05:46:56
【问题描述】:

我尝试使用 FirebaseAnimatedList() 使用搜索查询从 firebase 实时数据库中获取一些数据,该搜索查询应该是 query : ref.child("users").child(userPath).orderByChild("id").equalTo(userID) 但不是该查询在没有第二个孩子的情况下也可以像这样query : ref.child("users").orderByChild("id").equalTo(userID)

似乎在传递数据时.equalTo(userID) 如果在数据库中找不到传递的用户ID,它会加载一个空白容器我正在寻找一种方法来避免加载空白容器并在没有值的情况下返回自定义视图匹配.equalTo(userID)

实时数据库示例:

{
  "users" : {
    "user1" : {
      "lname" : "dee",
      "id" : "12"
    },
    "user2" : {
      "lname" : "nee",
      "id" : "13"
    },
    "user3" : {
      "lname" : "lee",
      "id" : "14"
    },
    "user4" : {
      "lname" : "bree",
      "id" : "15"
    }
  }

代码:

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 firebase-realtime-database dart


    【解决方案1】:

    查询实际上是正确的,要能够使用orderByChild("id") 访问id,您不需要使用另一个child() 方法。

    我正在寻找一种方法来避免加载空白容器并返回自定义视图

    FirebaseAnimatedList(
            query: ref.child("users").orderByChild("id").equalTo(this.userid),
            itemBuilder: (BuildContext context, DataSnapshot snapshot,
                Animation<double> animation, int index) {
             if(snapshot.value != null){
              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,
                    ),
                  ],
                ),
              );
             }
             else{
                 return Text("no Data");
                }
            return CircularProgressIndicator();
            }));
    

    【讨论】:

    • 非常感谢您的回复,但是当.equalTo(this.userid) 与数据库"id" 不匹配时它不起作用
    • 你有id : 12this.userid 是否等于经过身份验证的用户ID?如果是这样,那么确定它不会工作......你应该做.equalTo("12"),或者添加用户认证ID
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多