FirebaseAnimatedList中的嵌套子键查询

时间:2018-09-10 06:52:32

标签: android flutter

我的Firebase结构是这样的:

enter image description here

我想在FirebaseAnimatedList中获取“ groupName”列表。

如何实现?

我当前的代码:

chatGroupNameReference = FirebaseDatabase.instance.reference().child("chat_group_message_user_info").child(widget.user.uid);


body: new Container(
    child: new Flexible(
      child: new FirebaseAnimatedList(
        query: chatGroupNameReference,
        padding: const EdgeInsets.all(8.0),
        defaultChild: Center(child: CircularProgressIndicator(backgroundColor: Colors.deepPurpleAccent,),),
        itemBuilder: (_, DataSnapshot messageSnapshot,
            Animation<double> animation, int index) {

          return Text("Group Name will be here")
        },
      ),
    )
  )
);

2 个答案:

答案 0 :(得分:2)

对于嵌套子键查询,我们需要带有FirebaseAnimatedList的FutureBuilder:

chatGroupNameReference = FirebaseDatabase.instance.reference().child("chat_group_message_user_info").child(widget.user.uid);

body: new FirebaseAnimatedList(
    defaultChild: Center(child: new CircularProgressIndicator()),
    query: chatGroupNameReference,
    sort: (a,b) => (b.key.compareTo(a.key)),
    itemBuilder: (_, DataSnapshot messageSnapshot, Animation<double> animation, int index) {

      return new FutureBuilder<DataSnapshot>(
        future: chatGroupNameReference.child(messageSnapshot.key).once(),
        builder: (BuildContext context, snapshot){

          return snapshot.hasData ? new Text(snapshot.data.value['groupName']) : new Text("");
        },
      );
    },

答案 1 :(得分:1)

(_, DataSnapshot messageSnapshot,
        Animation<double> animation, int index)

在该行之后,您可以使用FutureBuilder从Firebase数据库获取数据。

例如In a firebase animated list, is there a way to let firebase list know the expected height of a widget before it loads?