【问题标题】:What is the problem with this flutter code. Im trying to read the data from firebase这个颤振代码有什么问题。我试图从 firebase 读取数据
【发布时间】:2022-02-09 16:19:58
【问题描述】:
Future<Object?> GetData() async {
  FirebaseDatabase database = FirebaseDatabase.instance;

  DatabaseReference ref = FirebaseDatabase.instance.ref("litre");

// Get the data once
  DatabaseEvent event = await ref.once();

// Print the data of the snapshot
  print(event.snapshot.value);
  return event.snapshot.value;
}

代码在这里我想获取“升”标签的值,然后将其打印到 Text() 函数中。 Firebase json 在这里 =>

{
    "litre": "16"
}

我预计会看到“16”,但我面临的是“未来”的实例

【问题讨论】:

  • 您必须提供一些日志或错误堆栈。或者解释一下你得到了什么输出而不是你期望的。
  • 正如@AbdullahZKhan 所说,您面临什么问题,请添加您的firebase 的快照。

标签: firebase flutter firebase-realtime-database


【解决方案1】:

GetData 是一个异步函数,因此在Text() 小部件中使用它会抛出异常,因为它不是字符串。

您可以使用FutureBuilder 构建Text() 小部件。

例子:

Otherwidgets,
FutureBuilder(
          future: GetData(),
          builder: (context, snapshot) => Text(snapshot.data.toString()),
        ),

【讨论】:

  • 如果你有时间,你能告诉我如何使用futurebuilder和文本小部件吗?
  • 编辑了答案
猜你喜欢
  • 2021-09-18
  • 2018-12-02
  • 2018-01-08
  • 2019-12-23
  • 2022-06-27
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多