Issue
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.
Solution
The first error is saying that doc.data["className"]
is null. Check your firestore and make sure that all the documents that you are loading have value on className
field.
The second error indicates that you have multiple documents with the same value of 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.
Answered By - dshukertjr
Answer Checked By - Candace Johnson (JavaFixing Volunteer)