I have an issue related to DropdownButtons I have in a flutter screen.
I have 6 DropdownButtons in a screen called trxn screen. This screen allows the user to enter data and save it to a Firebase document.
When I open the trxn screen it automatically scrolls to the bottom of the page. The last DropdownButton on the screen causes the screen to be red and I can’t see anything else above the red.
This is the code for one of the Dropdownbuttons:
Wrap(
children: [
const Text(
'Appraiser Company: ',
),
StreamBuilder<QuerySnapshot>(
stream: _db.collection('appraiserCompany').snapshots(),
builder:
(BuildContext context, AsyncSnapshot snapshot) {
List<DropdownMenuItem<String>> appraiserCompanyItems =
[];
appraiserCompanyItems.add(
const DropdownMenuItem<String>(
value: 'a',
child: Text('Select Appraiser Company')));
if (snapshot.hasData) {
final appraiserCompanyList = snapshot.data.docs;
for (var appraiserCompany in appraiserCompanyList) {
appraiserCompanyItems.add(
DropdownMenuItem(
value: appraiserCompany.id,
child: Text(
appraiserCompany['appraiserCompanyName'],
),
),
);
}
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
if (appraiserCompanyItems.isNotEmpty) {
return DropdownButton<String>(
hint: const Text("Select Appraiser"),
value: _selectedAppraiserCompany ??
appraiserCompanyItems[0].value,
onChanged: (appraiserCompanyValue) {
setState(() {
_selectedAppraiserCompany =
appraiserCompanyValue;
});
ref
.read(trxnNotifierProvider.notifier)
.updateAppraiserCompanyId(
_selectedAppraiserCompany!);
},
items: appraiserCompanyItems,
);
} else {
return const SizedBox();
}
}),
],
),
How do I get rid of the red screen? This is really frustrating.
2
Answers
It is difficult to give a diagnosis without understanding the error, but it occurs to me that you can try the following:
In the body of the Scaffold return the dropdown that you published here and check if it is displayed, if so, then the problem is in the widget tree when you have more than one dropdown (perhaps the Column or the ListView).
Instead of a Wrap, use a Column or a Row and see what happens.
Consider using Flexible or Expanded.
You can see that I simplified the initialization of appraiserCompanyItems using the List constructor. Also used snapshot.data!.docs.forEach to iterate over the documents directly. THis is important.
Also removed unnecessary condition for checking if appraiserCompanyItems is not empty before returning the DropdownButton widget.
Changed the initial value of the DropdownButton to ‘Select Appraiser’ if _selectedAppraiserCompany