【问题标题】:How to display one particular data from Realtime Database?如何显示实时数据库中的一个特定数据?
【发布时间】:2021-12-13 13:42:03
【问题描述】:

在 firebase 动画列表中,如何放入条件语句或其他任何内容,以便仅显示实时数据库中的一组数据?我目前可以在 ListTile 中显示所有这些,但我只想显示名称为“西班牙”的目的地及其描述,而不是包含西班牙、意大利、美国等的所有数据库。

class _TestDestinationsState extends State<TestDestinations> {
  final destdatabaseref = FirebaseDatabase.instance
      .reference()
      .child('Database')
      .child('Destinations');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xFF4D71AC),
        elevation: 0,
        centerTitle: true,
        title: Text('Eh',
            style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w500,
                color: Colors.white)),
      ),
      body: SafeArea(
          child: FirebaseAnimatedList(
        itemBuilder: (BuildContext context, DataSnapshot snapshot,
            Animation<double> animation, int index) {
          return SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(height: 15),
                Padding(
                  padding: const EdgeInsets.all(8),
                  child: ListTile(
                      title: Text(snapshot.value['name'],
                          style: TextStyle(fontSize: 20)),
                      subtitle: Text(snapshot.value['description'])),
                ),
              ],
            ),
          );
        },
        query: destdatabaseref,
      )),
    );
  }
}

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database


    【解决方案1】:

    如果我们只需要显示来自FirebaseDatabase的特定数据,我们可以使用以下逻辑:

    Visibility(
     visible: snapshot.value['name'] == 'Spain',
     child: ...
    ),
    

    完整的sn-p可以在下面找到:

    class _TestDestinationsState extends State<TestDestinations> {
      final destdatabaseref = FirebaseDatabase.instance
          .reference()
          .child('Database')
          .child('Destinations');
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Color(0xFF4D71AC),
            elevation: 0,
            centerTitle: true,
            title: Text('Eh',
                style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.w500,
                    color: Colors.white)),
          ),
          body: SafeArea(
              child: FirebaseAnimatedList(
            itemBuilder: (BuildContext context, DataSnapshot snapshot,
                Animation<double> animation, int index) {
              return Visibility(
               visible: snapshot.value['name'] == 'Spain',
               child: SingleChildScrollView(
                 child: Column(
                   children: [
                     SizedBox(height: 15),
                     Padding(
                       padding: const EdgeInsets.all(8),
                       child: ListTile(
                           title: Text(snapshot.value['name'],
                               style: TextStyle(fontSize: 20)),
                           subtitle: Text(snapshot.value['description'])),
                     ),
                   ],
                 ),
               ),
              );
            },
            query: destdatabaseref,
          )),
        );
      }
    }
    

    然而更好的解决方案是检索基于过滤器的特定数据,这可以通过将我们的查询过滤到FirebaseDatabase来完成,如下所示:

    final destdatabaseref = FirebaseDatabase.instance
        .reference()
        .child('Database')
        .child('Destinations')
        .orderByChild('name')
        .equalTo('Spain');
    

    【讨论】:

    • 嘿,非常感谢。通过可见性小部件进行操作,但对于通过过滤查询来检索数据,它给了我“位置参数过多:预期 1 个,但找到 2 个”的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多