【问题标题】:How to check if path in firebase is there( check if child is there ) flutter如何检查firebase中的路径是否存在(检查孩子是否存在)颤动
【发布时间】:2019-09-15 01:48:01
【问题描述】:

我想检查类似 (database.reference().child('classData')) 的路径是否存在。如果它不存在,我想返回 null

我试过了 (database.reference().child('classData') == null ) 在 if 语句中没有任何反应。

【问题讨论】:

  • 只需对该位置执行正常读取并检查该值,如果为 null 则表示它不存在

标签: android firebase firebase-realtime-database flutter


【解决方案1】:

database.reference().child('classData') 语句仅创建数据库中某个位置的路径。它不会触发对该路径的数据读取,因此不能用于检查该位置是否存在数据。

要确定数据是否存在,请从数据库中读取数据,例如使用once(),如example from the FlutterFire repo 所示:

_messagesRef = database.reference().child('messages');
database.reference().child('counter').once().then((DataSnapshot snapshot) {
  print('Connected to second database and read ${snapshot.value}');
});

然后检查DataSnapshotvalue

【讨论】:

  • 如果我再次运行它,读取一次就永远不会更新你知道原因吗?
猜你喜欢
  • 2016-09-18
  • 2016-09-20
  • 2017-02-25
  • 2017-10-24
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多