简体   繁体   English

Flutter 从 firebase 在 DropDown 中填充数据

[英]Flutter populating data in DropDown from firebase

I am learning flutter and followed a tutorial to populate data in dropdownlist from firebase FireStore.我正在学习 flutter 并按照教程从 firebase FireStore 的下拉列表中填充数据。 But during the process I am getting error:但在此过程中我收到错误:

Another exception was thrown: A non-null String must be provided to a Text widget.引发了另一个异常:必须向 Text 小部件提供非空字符串。

I am attaching the code, please let me know how to resolve this issue.我附上代码,请告诉我如何解决此问题。

//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

I found out that the error is firing from:我发现错误是从以下位置触发的:

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

this line, and to test, I changed it to Text("Sample"), then it gave error on line above这条线,为了测试,我把它改成了Text(“Sample”),然后它在上面的那行给出了错误

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

Error:错误:

Another exception was thrown: There should be exactly one item with [DropdownButton]'s value: 0.引发了另一个异常:应该恰好有一个项目具有 [DropdownButton] 的值:0。

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

The first error is saying that doc.data["className"] is null.第一个错误是说doc.data["className"]是 null。 Check your firestore and make sure that all the documents that you are loading have value on className field.检查您的 Firestore 并确保您正在加载的所有文档在className字段上都有值。

The second error indicates that you have multiple documents with the same value of doc.data["classid"] .第二个错误表明您有多个文档具有相同的doc.data["classid"]值。 Every value of DropdownMenuItem have to be unique, so make sure that you don't have same classid in any of the documents. 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(): hint, Text("Choose Category"): onChanged? (value) { setState(() { _chosenValue = value as String;; }), }, ); ); 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