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

后端 未结 2 1131
Happy的楠姐
Happy的楠姐 2020-12-18 07:38

I have a this Scaffold with body:

body: new Column(
        children: [
          new Flexible(
            child: new FirebaseAni         


        
相关标签:
2条回答
  • 2020-12-18 07:47

    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.

    In the previous example, I am doing the following query to only obtain contact of email set to "em1@gmail.com" 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("em1@gmail.com").endAt("em1@gmail.com"),
                    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("em1@gmail.com").endAt("em1@gmail.com") by equalTo("em1@gmail.com")

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

    For more information.

    0 讨论(0)
  • 2020-12-18 08:12

    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).

    0 讨论(0)
提交回复
热议问题