这个flutter代码有什么问题。我正在尝试从 firebase 读取数据

What is the problem with this flutter code. Im trying to read the data from firebase

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”,但我面对的是 'Future'

的实例

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

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

示例:

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