title: How to resolve flutter dropdownButtonFormField dynamic selection checking for dropdownButton's value
body: <p>I am trying to ask the user to select the type of item in the first dropdown and then select from its corresponding available colours in the second dropdown. However, when after I have selected a colour (i.e. white) and now want to switch to another item that does not have this colour, an error is thrown:</p>
  <pre><code>&quot;There should be exactly one item with [DropdownButton]'s value: white. \nEither zero or 2 or more
  [DropdownMenuItem]s were detected with the same value&quot;
  </code></pre>
  <p>Please help, I have already tried to setState at various places to update the values but this error still occurs.</p>
  <p>The following is my code snippet:</p>
  <pre><code>StreamBuilder&lt;QuerySnapshot&gt;(
      stream: mainItemsSnapshots,
      builder: (context, snapshot) {
      if (snapshot.hasError) return Text(&quot;Error&quot;);
      switch (snapshot.connectionState) {
          case ConnectionState.waiting:
          return Center(child: CircularProgressIndicator());
          default:
          {
              List&lt;DropdownMenuItem&gt; dropdownMenuItems =
                  snapshot.data.documents
                      .map((DocumentSnapshot mainItem) {
              return new DropdownMenuItem&lt;String&gt;(
                  value: mainItem.documentID,
                  child: Text(mainItem.documentID),
              );
              }).toList();
              return DropdownButtonFormField&lt;String&gt;(
              items: dropdownMenuItems,
              onChanged: (String value) {
                  if (value != tempMainItemType) {
                      setState(() {
                      tempMainItemType = value;
                      tempItemColorsList.clear();
                      tempItemColorsList = [];
                      tempMainItemColor = null;
                      });
                  }
                  
                  if (tempItemColorsList.isEmpty &amp;&amp; value != null) {
                  tempItemColorsList = snapshot.data.documents
                      .where((element) =&gt; element.documentID == value)
                      .first
                      .data[&quot;colors&quot;]
                      .keys
                      .map((color) =&gt; color.toString())
                      .toList()
                      .cast&lt;String&gt;();
                  }
                  setState((){});
              },
              onSaved: (String value) {
                  _order.mainItemType = value;
              },
              value: tempMainItemType,
              );
          }
      }
      },
  ),
  
  // Main color
  if (tempItemColorsList?.isNotEmpty)
      Padding(
      padding: const EdgeInsets.only(top: spacingGeneral),
      child: textFieldLabel(context, &quot;Main color&quot;),
      ),
  if (tempItemColorsList?.isNotEmpty)
      DropdownButtonFormField(
      items: tempItemColorsList.map((String color) {
          return new DropdownMenuItem&lt;String&gt;(
          value: color,
          child: Text(color),
          );
      }).toList(),
      onSaved: (String value) {
          _order.mainColor = value;
      },
      value: tempMainItemColor,
      onChanged: (String value) {
          setState(() {
          tempMainItemColor = value;
          });
      },
      ),
  </code></pre>