【发布时间】:2021-03-25 12:19:10
【问题描述】:
我想从 Firebase 检索一个子文档,我需要收集特定用户在他们的个人资料页面上提出和回答的问题数量,我正在成功检索问题数量,因为它在主文档中,@ 987654321@,但我无法检索答案,它是问题的子文档image 2 sub doc,我尝试从 AsyncSnaphot - DocumentSnapshot - 动态更改,但在任何地方都没有运气。提前谢谢你。
```Widget followers(
BuildContext context, AsyncSnapshot<DocumentSnapshot> asyncSnapshot) {
return Container(
color: constantColors.green,
height: MediaQuery.of(context).size.height*0.09,
child: Row(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('questions')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Container();
} else {
return Provider.of<Authentication>(context, listen: false)
.getUserUid ==
asyncSnapshot.data.data()['user uid']
? Text(snapshot.data.docs.length.toString(), style: TextStyle(fontSize: 24),)
: Text('0');
}
}),
Text('asked'),
],
),
Column(
children: [
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('questions').doc().collection('answers')
.snapshots(),
builder: (context, snapshot){
if (snapshot.connectionState == ConnectionState.waiting) {
return Container();
} else {
return Provider.of<Authentication>(context, listen: false)
.getUserUid ==
asyncSnapshot.data.data()['user uid']
? Text(snapshot.data.docs.length.toString(), style: TextStyle(fontSize: 24),)
: Text('0');
}
}),
Text('answered')
],
)
],
),
);
} ```
编辑:将我的答案的更多代码添加到 Firebase 功能中
answerAdder(BuildContext context,
AsyncSnapshot<DocumentSnapshot> documentSnapshot, String QuesID) {
return showModalBottomSheet(
elevation: 0,
backgroundColor: Colors.transparent,
isScrollControlled: true,
context: context,
builder: (context) {
return Container(
height: MediaQuery.of(context).size.height * 0.8,
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(horizontal: 5),
decoration: BoxDecoration(
color: constantColors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(25), topLeft: Radius.circular(25)),
),
child: Column(
children: [
Divider(
indent: 110,
endIndent: 110,
thickness: 4,
color: Colors.grey,
),
Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
MaterialButton(
onPressed: () {
Provider.of<PostFunctions>(context, listen: false)
.addAnswer(
context,
documentSnapshot.data.data()['question id'],
answerController.text,
titleController.text)
.whenComplete(() {
Navigator.pop(context);
});
Navigator.pop(context);
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
color: constantColors.cyangrad,
child: Text(
'add answer',
style: TextStyle(fontWeight: FontWeight.w600),
),
)
],
),
),
Container(
height: MediaQuery.of(context).size.height * 0.2,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: TextField(
controller: answerController,
maxLines: 8,
cursorColor: constantColors.green,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(15),
hintText: 'Please enter your answer',
isDense: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide.none),
fillColor: Colors.grey[300],
filled: true),
),
),
),
Container(
height: MediaQuery.of(context).size.height * 0.2,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: TextField(
controller: titleController,
maxLines: 8,
cursorColor: constantColors.green,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(15),
hintText: 'Please enter your title',
isDense: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide.none),
fillColor: Colors.grey[300],
filled: true),
),
),
),
],
),
);
});}
上传答案的未来方法
Future addAnswer(BuildContext context, String postId,
String answer, String answerTitle) async {
return FirebaseFirestore.instance
.collection('questions')
.doc(postId)
.collection('answers')
.doc(answerTitle)
.set({
'answer': answer,
'username': Provider.of<FirebaseOps>(context, listen: false).initUsername,
'user uid':
Provider.of<Authentication>(context, listen: false).getUserUid,
'userimage':
Provider.of<FirebaseOps>(context, listen: false).initUserimage,
'useremail':
Provider.of<FirebaseOps>(context, listen: false).initUseremail,
'time': Timestamp.now(),
});
}
【问题讨论】:
标签: firebase flutter google-cloud-firestore