FirebaseAnimatedList query when query don't have data to fetch

Viewed 847

I'm super new to flutter and firebase and trying to query some data from the firebase realtime database using the FirebaseAnimatedList in the example code below I try to pass this.userid to fetch data according to the value being passed but sometimes database might not have data according to what's being passed. I just want to know how to handle such cases and show a feedback saying there's no data that matched the query. It's basically a search where it might not have values that user look for.

example code:


    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,
                    ),
                  ],
                ),
              );
            }));
2 Answers

snapshot is of type DataSnapshot, therefore you can check if data exists or not by using the property 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,
                    ),
                  ],
                ),
              );
             } 
            }

Check the following for more info:

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

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,
        ),
      ],
    ),
  );
}));
Related