【问题标题】:How can i fetch a field from a specific only我怎样才能从特定的只获取一个字段
【发布时间】:2021-10-26 16:16:17
【问题描述】:

我在 Firebase 实时数据库中有这个简单的数据:

现在我需要使用以下代码获取数据:

Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.deepPurple,
      appBar: AppBar(),
      body:  SingleChildScrollView(
        child: FirebaseAnimatedList(
          shrinkWrap: true,

          query: FirebaseDatabase.instance.reference(),
          itemBuilder: (BuildContext context, DataSnapshot snapshot,Animation<double> animation,  int index) {
            return Text(snapshot.value["name"]);
          },),
      ),
    );
  }

使用此代码,我将在 UI 中获取两个字段,即 [name:Andy] 和 [name:Derek]

更多解释我截图了这个数据

但是我怎样才能只获取一个字段?假设我只需要获取 User 1 child 下的 [name : Andy]。

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database


    【解决方案1】:

    尝试以下方法:

    FirebaseDatabase.instance.reference().child("User 1");
    

    这将返回User 1 对象

    【讨论】:

    • 我试过这个它给出了错误,说 Class bool has no instance methood [] reciver true trird calling [] ("name")
    • @AslanMahy 试试Text(snapshot.value.toString()) 告诉我你得到了什么
    • 是它的工作,但它获取用户 1 下的所有字段 .. 活动字段 + 名称 .. 我只能获取名称吗? .. 非常感谢
    • 当然,您可以使用child("User 1/name") 仅获取用户名。
    • 如果你的意思是这样 FirebaseDatabase.instance.reference().child("User 1/name") ..... Text(snapshot.value.toString()) 什么都没有获取..空白屏幕
    猜你喜欢
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2021-04-05
    • 2020-10-07
    相关资源
    最近更新 更多