繁体   English   中英

Flutter 从 firebase 在 DropDown 中填充数据

[英]Flutter populating data in DropDown from firebase

我正在学习 flutter 并按照教程从 firebase FireStore 的下拉列表中填充数据。 但在此过程中我收到错误:

引发了另一个异常:必须向 Text 小部件提供非空字符串。

我附上代码,请告诉我如何解决此问题。

//Code Above
            Container(
              padding: EdgeInsets.all(5),
              child: StreamBuilder<QuerySnapshot>(
                stream: repository.getStream(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData)
                    return const Center(
                      child: const CupertinoActivityIndicator(),
                    );

                  return Container(
                    padding: EdgeInsets.all(5),
                    child: new DropdownButton(
                      value: _dropdownValue,
                      isDense: true,
                      items:
                          snapshot.data.documents.map((DocumentSnapshot doc) {
                        return new DropdownMenuItem<int>(
                            value: doc.data["classid"] as int,
                            child: Text(doc.data["className"]));
                      }).toList(),
                      hint: Text("Choose Class"),
                      onChanged: (value) {
                        setState(() {
                          _dropdownValue = value;
                        });
                      },
                    ),
                  );
                },
              ),
            ),
//Code Below

我发现错误是从以下位置触发的:

孩子:文本(doc.data[“className”]));

这条线,为了测试,我把它改成了Text(“Sample”),然后它在上面的那行给出了错误

值:doc.data["classid"] as int,

错误:

引发了另一个异常:应该恰好有一个项目具有 [DropdownButton] 的值:0。

数据库: 在此处输入图像描述

第一个错误是说doc.data["className"]是 null。 检查您的 Firestore 并确保您正在加载的所有文档在className字段上都有值。

第二个错误表明您有多个文档具有相同的doc.data["classid"]值。 DropdownMenuItem的每个值都必须是唯一的,因此请确保您在任何文档中都没有相同的classid

return Container( padding: EdgeInsets.all(5), child: DropdownButton( value: _chosenValue, isDense: true, items: snapshot.data..docs:map((DocumentSnapshot doc) { return DropdownMenuItem( value.doc,data[' title']: child.Text(doc;data['title'])). }),toList(): 提示, Text("Choose Category"): onChanged? (value) { setState(() { _chosenValue = value作为字符串;; }), }, ); );

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM