Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query in a Flutter/FirebaseAnimatedList using buildArguments or anything else? (example please)

I have a this Scaffold with body:

body: new Column(
        children: <Widget>[
          new Flexible(
            child: new FirebaseAnimatedList(
              query: FirebaseDatabase.instance
                  .reference()
                  .child('products')
                  .orderByChild('order'),
              padding: new EdgeInsets.all(8.0),
              reverse: false,
              itemBuilder: (_, DataSnapshot snapshot,
                  Animation<double> animation, int x) {
                return new ProductItem(
                    snapshot: snapshot, animation: animation);
              },
            ),
          ),
        ],
      ),

but I need to add a simple query: 'Active' = true

Is it possible? How? Any example/tutorial?

Thanks.

like image 587
abnerh69 Avatar asked Oct 24 '17 14:10

abnerh69


People also ask

What is animatedlist in flutter and how to use it?

AnimatedList is a built-in widget in Flutter, used to implement a list view that animates its items when they are inserted or removed. This helps the user feel less sudden and more pleasant about the change of the list. In this article, you will learn how to use AnimatedList through a couple of complete examples.

How to save a list to Firebase from a form widget?

Then inside the form widget, we use a TextFormField: The TextFormField will let us, use the validator property that validates an input. The full code for main.dart: To save the list to firebase, we first need an random id that can seperate the data. Fortunately, firebase provides the method push () which will do just that:

What is the use of then() callback in Firebase?

The then () callback will execute after the data is added in the Firebase database, and you can use the catchError which will handle errors emitted by this Future. (This is the asynchronous equivalent of a “catch” block). The firebase realtime database: To retrieve the list, first we need to get a reference to the node.

What is a futurebuilder in flutter?

You can learn more about it here, basically the FutureBuilder will make it easily to use the result of an asynchronous call with a flutter widget. We assign dbRef.once () which returns a Future<DataSnapshot> to the future property, this will retrieve the data only once.


2 Answers

If I am getting your question correctly, you are trying to query some data where ("Active" = true), then see the following example.

I have added a screenshot from my db to have more context on the way my data is structured and hopefully it will put you into perspective to implement something similar on your end.

enter image description here

In the previous example, I am doing the following query to only obtain contact of email set to "[email protected]" while neglecting the others.

 @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text("Firebase Example"),),
      body: new Column(
        children: <Widget>[
          new Flexible(
            child: new FirebaseAnimatedList(
                query: FirebaseDatabase.instance
                    .reference().child("contacts")
                    .orderByChild("email")
                    .startAt("[email protected]").endAt("[email protected]"),
                padding: new EdgeInsets.all(8.0),
                reverse: false,
                itemBuilder: (_, DataSnapshot snapshot,
                    Animation<double> animation, int x) {
                  return new ListTile(
                    subtitle: new Text(snapshot.value.toString()),
                  );
                }
            ),
          ),
        ],
      ),
    );
  }

Hope it helped.

P.S: As Chenna Reddy pointed out, you can replace startAt("[email protected]").endAt("[email protected]") by equalTo("[email protected]")

startAt and endAt are useful when you need to limit your query to a certain range.

For more information.

like image 164
Shady Aziza Avatar answered Nov 03 '22 00:11

Shady Aziza


Ok, following your guide (@ChennaReddy and @aziza), this is the way I fix my code:

body: new Column(
        children: <Widget>[
          new Flexible(
            child: new FirebaseAnimatedList(
              query: FirebaseDatabase.instance
                  .reference()
                  .child('products')
                  .orderByChild('Active')           // line changed
                  .equalTo(true),                   // line added
              padding: new EdgeInsets.all(8.0),
              reverse: false,
              itemBuilder: (_, DataSnapshot snapshot,
                  Animation<double> animation, int x) {
                return new ProductItem(
                    snapshot: snapshot, animation: animation);
              },
            ),
          ),
        ],
      ),

However, I need the resulting query order by 'order' which is a integer field (or attribute) that some admin users can change (update). So final users can see data ordered by 'order' (but only 'Active'=true as you help me to solve).

like image 33
abnerh69 Avatar answered Nov 03 '22 01:11

abnerh69