DropdownFlutterWidgets

There should be exactly one item with DropdownButton

In Flutter every UI component is a widget. These widgets are the building blocks of a complete screen, to which user has to interact to perform his task. Dropdown widget is one of them. During developing user interface developers often face many problems. A Dropdown  button in Flutter also can rise issues for developer if he did not use it according to its documentation. In this tutorial we will learn how to solve the very common error of Flutter dropdown widget ‘ There should be exactly one item with DropdownButton ’ . This is a very common issue while using dropdown button in our user interface in Flutter applications.

Dropdown Button Error in Flutter

Assertionfailed:file:///Users/naeemtariq/Desktop/Developer/flutter/packages/flutter/lib/sc/material/dropdown.dart:892:15

items == null I items.isEmpty |I value ==null Ilitems.where ( (DropdownMenultem<T>item) {

return item.value == value;)).length ==1

“There should be exactly one item with[DropdownButton’s value: Fourth Item.\nEither zero or 2 or more[DropdownMenultem]s were detectedwith the same value”

This is a common error that a developer can face during the use of Dropdown  button widget in Flutter App. To understand the reason of this error, first we have to understand the anatomy of properties of Dropdown button widget.

Properties of Dropdown Button in Flutter

In DropdownButton, there are two essential properties that we have to pass to its constructor. First one is items and second one is value.

1-Items in Dropdown Button in Flutter

The items property in Dropdown Button is a List of DropdownMenuItem widget. This is the list of items from which user has to select any one item. This DropdownMenuItem also has two essentials properties. First one is child and second one is value.

1-Child property in DropdownMenuItem

The type of child property is widget. We can pass Text to this property. This text will show the options to user. Second property is value.

2-Value property in DropdownMenuItem

This value is very important in DropdownMenuItem widget. We have to pass some value to it. This value should be unique.  If two DropdownMenuItem have same value. Then we will face that issue ‘ There should be exactly one item with DropdownButton ’.

2-Value property of Dropdown Button in Flutter

When we use the Dropdown Button in our user interface. We have to set the initial value of Dropdown button. This value will be selected by default when user will open the screen and Dropdown Button widget will be render.

Solution of this error ‘ There should be exactly one item with DropdownButton ’

Above we understand the most important properties of Dropdown Button Widget in Flutter. Now we will see the two reasons of this common error.

1- Duplicate value in DropdownMenuItem List  

As we have read that DropdownButton has the items property. Which is the list of DropdownMenuItem widgets. And each DropdownMenuItem widget has a property of value. This value should be unique. If any value is duplicated in it, then You will face this problem. So make sure that each value is exactly once in the DropdownMenuItem list.

2- Value exists in DropdownMenuItem List

Now we will discuss the value property of DropdownButton widget. This is the value that will allow the Dropdown to show the default selected value. This value must be exist in one of values of any DropdownMenuItems that we have pass as items in DropdownButton.

By avoiding these two mistakes, You can overcome the chances of facing this problem during the use of DropdownButton in you application

Conclusion

In this tutorial we see that what mistakes we do by which we face issues with DropdownMenuButton. By avoiding these two mistakes, we can overcome the chances of facing this problem during the use of DropdownButton in our application. Hope you have understand the reasons and their solution that how to solve this error. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *