2020年12月31日星期四

Flutter populating data in DropDown from firebase

I am learning flutter and followed a tutorial to populate data in dropdownlist from 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.

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"]));

this line, and to test, I changed it to Text("Sample"), then it gave error on line above

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

Error :

Another exception was thrown: There should be exactly one item with [DropdownButton]'s value: 0.

https://stackoverflow.com/questions/65526645/flutter-populating-data-in-dropdown-from-firebase January 01, 2021 at 09:05AM

没有评论:

发表评论