如何使用方法从Firebase实时数据库获取单个值
这是我的方法,我打算使用它将数据从数据库提取到文本小部件如何使用方法从Firebase实时数据库获取单个值,firebase,flutter,firebase-realtime-database,Firebase,Flutter,Firebase Realtime Database,这是我的方法,我打算使用它将数据从数据库提取到文本小部件 getSnapshotData (String child){ final User _firebaseUser = firebaseAuth.currentUser; databaseReference .child("users") .child(_firebaseUser.uid) .once() .then((DataSnapshot dataSn
getSnapshotData (String child){
final User _firebaseUser = firebaseAuth.currentUser;
databaseReference
.child("users")
.child(_firebaseUser.uid)
.once()
.then((DataSnapshot dataSnapshot) {
return dataSnapshot.value('${child}');
}
);
这是我的文本小部件,用于从数据库检索名称
Text(
'Hi,' + ' ${getSnapshotData('name')}',
style: TextStyle(
color: Colors.black,
fontSize: 32,
)),
Text(
getSnapshotData('orders_count').toString(),
style: TextStyle(
color: Colors.black,
fontSize: 32,
)),
这是我的文本小部件,用于从数据库检索订单计数
Text(
'Hi,' + ' ${getSnapshotData('name')}',
style: TextStyle(
color: Colors.black,
fontSize: 32,
)),
Text(
getSnapshotData('orders_count').toString(),
style: TextStyle(
color: Colors.black,
fontSize: 32,
)),
我尝试记录订单计数和名称的值以及几乎所有内容,一切似乎都很好我不明白问题出在哪里我花了三个多小时试图寻找解决方案,但失败了
使用Future Builder获取数据
return Container(
child: FutureBuilder(
future: getPosts(),
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text("Loading..."),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
return Column(
children: <Widget>[
Text(
snapshot.data[index].data['your_data']
.toString(),
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500),
),]
),}
),
});
首先创建getPost()
方法以返回qn
Future getPosts() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("your_collecttion_name").getDocuments();
return qn.documents;
}
使用后,future builder将获取数据
return Container(
child: FutureBuilder(
future: getPosts(),
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text("Loading..."),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
return Column(
children: <Widget>[
Text(
snapshot.data[index].data['your_data']
.toString(),
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500),
),]
),}
),
});
返回容器(
孩子:未来建设者(
future:getPosts(),
构建器:(\uux,快照){
if(snapshot.connectionState==connectionState.waiting){
返回中心(
子项:文本(“加载…”),
);
}否则{
返回ListView.builder(
itemCount:snapshot.data.length,
itemBuilder:(\ux,索引){
返回列(
儿童:[
正文(
snapshot.data[index].data['your_data']
.toString(),
样式:TextStyle(
尺寸:20,
fontWeight:fontWeight.w500),
),]
),}
),
});
使用Future Builder获取数据
return Container(
child: FutureBuilder(
future: getPosts(),
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text("Loading..."),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
return Column(
children: <Widget>[
Text(
snapshot.data[index].data['your_data']
.toString(),
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500),
),]
),}
),
});
首先创建getPost()
方法以返回qn
Future getPosts() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("your_collecttion_name").getDocuments();
return qn.documents;
}
使用后,future builder将获取数据
return Container(
child: FutureBuilder(
future: getPosts(),
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text("Loading..."),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
return Column(
children: <Widget>[
Text(
snapshot.data[index].data['your_data']
.toString(),
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500),
),]
),}
),
});
返回容器(
孩子:未来建设者(
future:getPosts(),
构建器:(\uux,快照){
if(snapshot.connectionState==connectionState.waiting){
返回中心(
子项:文本(“加载…”),
);
}否则{
返回ListView.builder(
itemCount:snapshot.data.length,
itemBuilder:(\ux,索引){
返回列(
儿童:[
正文(
snapshot.data[index].data['your_data']
.toString(),
样式:TextStyle(
尺寸:20,
fontWeight:fontWeight.w500),
),]
),}
),
});
hey抱歉延迟回复am in africa internet已关闭我没有使用firestore am使用实时数据库,您使用的语法用于firestore hey抱歉延迟回复am in africa internet已关闭我没有使用firestore am使用实时数据库,您使用的语法用于firestore