简体   繁体   English

我想在未来的构建器中显示集合的文档 ID,但出现错误

[英]I want to display document ids of collection in future builder but i got error

Below is the code in which i want to display documents ids from cloud firestore but i got error.下面是我想在其中显示来自云 Firestore 的文档 ID 的代码,但出现错误。 Error picture is also attached below错误图片也附在下面

FutureBuilder<QuerySnapshot>(
                  future: db.collection('/Exam').get(),
                  builder:
                      (BuildContext context, AsyncSnapshot asyncSnapshot) {
                    if (asyncSnapshot.hasError)
                      return Text("Error: ${asyncSnapshot.error}");
                    if (!asyncSnapshot.hasData)
                      return const CircularProgressIndicator();

                    return DropdownButton<String>(
                      isExpanded: true,
                      items: asyncSnapshot.data!.docs
                          .map(
                            (snap) => DropdownMenuItem(
                              value: snap.id,
                              child: Text(
                                snap.id.toString(),
                              ),
                            ),
                          )
                          .toList(),
                      value: _selectedexam,
                      onChanged: (String? newValue) {
                        setState(() {
                          _selectedexam = newValue!;
                          _selectedsemester = null;
                          print(_selectedexam);
                        });
                      },
                    );
                  }),

here is my error picture这是我的错误图片

class _MyHomePageState extends State<MyHomePage> {
final docs = ... 
String selectedId;

@overrid
initState(){
   super.initState()
   selectedId = docs.first.id;
}

 @override
 Widget build(BuildContext context) {
  final items = docs.map<DropdownMenuItem<String>>((Document value) {
        return DropdownMenuItem<String>(
          value: value.id,
          child: Text(value.id),
        );
      }).toList();

  return Container(
    child: DropdownButton<String>(
        value: selectedId,
        onChanged: (String? value) {
          setState(() {
            selectedId = value!;
          });
        },
        items: items,
      )
     ),
    );
  }
}

I edited directly in stackoverflow editor, so I may have made typos.我直接在stackoverflow编辑器中编辑,所以我可能打错了。 Also consider using a FutureBuilder as suggested by @Jacob还可以考虑使用 @Jacob 建议的 FutureBuilder

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想使用 flutter 中的未来构建器在下拉按钮中显示来自云 Firestore 的文档 ID,但出现错误 - I want to Display document ids from cloud firestore in drop down button using future builder in flutter but got error 我想在特定日期将旧文档添加到新集合 - I want to add a old document to a new collection on a specific date _Future 的实例<int>当我试图在 Firestore 中获取我的“收藏”的全部“文档”时,我得到的就是这些</int> - Instance of _Future<int> is all I get when I try to get the total 'document' of my 'collection' in Firestore 我正在尝试获取用户简历并将其写入另一个集合中的另一个文档中,但我得到了一个未来的实例<dynamic>在我的数据库中</dynamic> - I am trying to get a user bio and write it in another document in a different collection but I get an Instance of future<dynamic> in my database 参数类型“用户?” 不能分配给参数类型“未来<object?> ?'.,我什么时候可以让代码得到这个错误</object?> - The argument type 'User?' can't be assigned to the parameter type 'Future<Object?>?'., when i can the code got this error Angular Firestore - 我想获取集合的文档 ID 并将它们转换为数组 - Angular Firestore - I want get document ID's of collection and convert them to an array 尝试使用带有 azure python SDK 的异步启动多个 ADf 触发器,我得到 RuntimeError: got Future<future pending> 附加到不同的循环</future> - Trying to start several ADf triggers using asyncio with azure python SDK, I get RuntimeError: got Future <Future pending> attached to a different loop 如何从 Firebase Firestore 集合中指定文档的特定字段获取数据并将其显示到我的 UI? - How do I get data from a specific field of the specified document inside a Firebase Firestore collection and display it to my UI? 如何更新集合中的文档 firebase - How can I Update Document in Collection firebase 我不想在 python 中写入和读取相同的文档 - I do not want to write and read the same document in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM