如何检索firebase子文档-颤振

如何检索firebase子文档-颤振,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我想从Firebase检索子文档,我需要收集特定用户在其个人资料页面上询问和回答的问题数量,我正在成功检索问题数量,因为它位于主文档中,但我无法检索答案,它是问题的子文档,我尝试过从AsyncSnaphot-DocumentSnapshot-dynamic进行更改,但在任何地方都没有成功。先谢谢你 ```Widget followers( BuildContext context, AsyncSnapshot<DocumentSnapshot> asyncSnapshot) {

我想从Firebase检索子文档,我需要收集特定用户在其个人资料页面上询问和回答的问题数量,我正在成功检索问题数量,因为它位于主文档中,但我无法检索答案,它是问题的子文档,我尝试过从AsyncSnaphot-DocumentSnapshot-dynamic进行更改,但在任何地方都没有成功。先谢谢你

```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')

        ],
      )
    ],
  ),
);
``小部件跟随者(
BuildContext上下文,异步快照(异步快照){
返回容器(
颜色:君士坦丁堡绿,
高度:MediaQuery.of(上下文).size.height*0.09,
孩子:排(
儿童:[
纵队(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
StreamBuilder(
流:FirebaseFirestore.instance
.collection(“问题”)
.snapshots(),
生成器:(上下文,快照){
if(snapshot.connectionState==connectionState.waiting){
返回容器();
}否则{
返回Provider.of(上下文,侦听:false)
.getUserUid==
asyncSnapshot.data.data()['user uid']
文本(snapshot.data.docs.length.toString(),样式:TextStyle(fontSize:24),)
:文本('0');
}
}),
文本(“询问”),
],
),
纵队(
儿童:[
StreamBuilder(
流:FirebaseFirestore.instance
.collection('questions').doc().collection('answers'))
.snapshots(),
生成器:(上下文,快照){
if(snapshot.connectionState==connectionState.waiting){
返回容器();
}否则{
返回Provider.of(上下文,侦听:false)
.getUserUid==
asyncSnapshot.data.data()['user uid']
文本(snapshot.data.docs.length.toString(),样式:TextStyle(fontSize:24),)
:文本('0');
}
}),
文本('已回答')
],
)
],
),
);
}```

编辑:添加更多“我的答案”代码添加到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),
                ),
              ),
            ),
          ],
        ),
      );
    });}
answerAdder(构建上下文,
AsyncSnapshot文档快照,字符串QuesID){
返回showModalBottomSheet(
海拔:0,
背景颜色:颜色。透明,
是的,
上下文:上下文,
生成器:(上下文){
返回容器(
高度:MediaQuery.of(上下文).size.height*0.8,
宽度:MediaQuery.of(context).size.width,
边缘:边缘组。对称(水平:5),
装饰:盒子装饰(
颜色:君士坦丁堡白色,
borderRadius:仅限borderRadius(
右上:半径。圆形(25),左上:半径。圆形(25)),
),
子:列(
儿童:[
分隔器(
缩进:110,
结束缩进:110,
厚度:4,
颜色:颜色。灰色,
),
填充物(
填充:常数边集全部(5.0),
孩子:排(
mainAxisAlignment:mainAxisAlignment.end,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
材料按钮(
已按下:(){
Provider.of(上下文,侦听:false)
.addAnswer(
上下文
documentSnapshot.data.data()['question id'],
answerController.text,
titleController.text)
.完成时(){
Navigator.pop(上下文);
});
Navigator.pop(上下文);
},
形状:圆形矩形边框(
边界半径:边界半径。圆形(15)),
颜色:constantColors.cyangrad,
子:文本(
“添加答案”,
样式:TextStyle(fontWeight:fontWeight.w600),
),
)
],
),
),
容器(
高度:MediaQuery.of(context).size.height*0.2,
孩子:填充(
填充:常数边集全部(5.0),
孩子:TextField(
控制员:应答控制员,
maxLines:8,
cursorColor:ConstantColor.green,
装饰:输入装饰(
内容填充:边缘集。全部(15),
hintText:'请输入您的答案',
是的,
enabledBorder:OutlineInputBorder(
边界半径:边界半径。圆形(20),
边界边:边界边。无),
聚焦顺序:大纲输入边框(
边界半径:边界半径。圆形(20),
边界边:边界边。无),
fillColor:颜色。灰色[300],
填写:正确),
),
),
),
容器(
高度:MediaQuery.of(context).size.height*0.2,
孩子:填充(
填充:常数边集全部(5.0),
孩子:TextField(
控制器:标题控制器,
maxLines:8,
cursorColor:ConstantColor.green,
装饰:输入装饰(
内容填充:边缘集。全部(15),
hintText:'请输入您的标题',
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(),
});