【发布时间】:2021-09-11 06:17:27
【问题描述】:
我是一名新的 Flutter 开发人员。目前正在从我的 FBDB 访问数据。 我调用了一个看起来像这样的函数。
void initState() {
super.initState();
Future<PartInfo> partInfo = HelperMethods.getPartDeets(widget.partid)
}
HelperMethods 的代码如下所示。
class HelperMethods {
static Future<PartInfo> getPartDeets(String partid) async {
PartInfo partInfo ;
DatabaseReference partRef = FirebaseDatabase.instance.reference().child('parts/id0001');
partRef.get().then((DataSnapshot snapshot) {
if (snapshot.value != null) {
partInfo = PartInfo.fromSnapshot(snapshot); //globalvar currentUserInfo
print('part is ${partInfo.partname}');
return partInfo;
} else {
print('snapshot is null') ;
}
});
print(partInfo);
return partInfo;
}
}
我的数据库中的数据如下所示:
getPartDeets 方法中的打印语句永远不会被命中。并且 print(partInfo) 返回 null。有关如何解决此问题的任何提示?
【问题讨论】:
-
代替
partRef.get().then..试试partRef.once().then..
标签: flutter asynchronous firebase-realtime-database