Flutter:访问Firebase实时数据库中的数据 [英] Flutter: accessing data in Firebase Realtime Database
问题描述
我将以下Firebase实时数据库连接到我的flutter项目:
I have the following Firebase realtime database connected to my flutter project:
我对flutter和firebase还是比较陌生,我了解在flutter项目中访问此数据库中数据的一种方法是使用数据库引用变量.所以我有以下参考变量 itemRef
:
I am fairly new to flutter and firebase, I understand that a way to access the data in this database in my flutter project is by using a database reference variable. So i have the following reference variable itemRef
:
class _MyHomePageState extends State<MyHomePage> {
DatabaseReference itemRef;
@override
void initState() {
// TODO: implement initState
super.initState();
itemRef = FirebaseDatabase.instance.reference();
}
现在,如何使用itemItem访问数据库中的温度和脉冲值?
Now, how do i access the temperature and pulse values in the database using this itemRef?
推荐答案
尝试以下操作:
itemRef = FirebaseDatabase.instance.reference();
itemRef.once().then((DataSnapshot snapshot){
print(snapshot.value);
var pulse = snapshot.value["pulse"];
var temp = snapshot.value["temperature"];
});
使用 once()
方法检索数据,然后 snapshot.value
将为 Map< String,dynamic>
类型,您可以使用 get
运算符访问属性.
use the once()
method to retrieve the data, then snapshot.value
will be of type Map<String,dynamic>
and you can access the attributes using the get
operator.
这篇关于Flutter:访问Firebase实时数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!