You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter DropdownButton报错求助:值为Select district的选项需唯一

Fixing the "There should be exactly one item with [DropdownButton]'s value" Error in Flutter

Let's break down exactly what this error means first: your DropdownButton's current value ("Select district" in your case) either doesn't exist at all in your list of DropdownMenuItems, or it appears more than once. Since you mentioned your API is working and data is loading into the list, let's walk through the most common fixes for this issue:

1. Ensure Your Default Value Has a Matching Menu Item

If you're using "Select district" as your initial selected value, you must include this exact value as a DropdownMenuItem in your items list. It's easy to forget this step, which triggers the error immediately. Here's a concrete example:

// Define your selected value state
String selectedDistrict = "Select district";

DropdownButton<String>(
  value: selectedDistrict,
  items: [
    // Add the default "Select district" item first
    const DropdownMenuItem(
      value: "Select district",
      child: Text("Select district"),
    ),
    // Then add your API-loaded districts
    ...yourDistrictList.map((district) => DropdownMenuItem(
          value: district.name, // Or district.id—just keep it consistent!
          child: Text(district.name),
        )).toList(),
  ],
  onChanged: (newValue) {
    setState(() {
      selectedDistrict = newValue!;
    });
  },
)

2. Fix Value Mismatches (Type or Content)

Another frequent culprit is a mismatch between your selectedDistrict type/value and the value of your DropdownMenuItems:

  • Type consistency: If selectedDistrict is a String, make sure all DropdownMenuItem values are also Strings (don’t mix with int for district IDs, for example).
  • Content consistency: Check for typos, extra spaces, or case differences. If your API returns "Beijing" but your selected value is "beijing", they won’t register as a match.

3. Use hint Instead of a Default Value (Cleaner Approach)

If you don’t want a pre-selected placeholder item, use the hint property instead of setting a value that doesn’t exist in your item list. This avoids the error entirely because the value will be null until the user selects an option:

// Make selected value nullable
String? selectedDistrict;

DropdownButton<String>(
  value: selectedDistrict,
  // Shows when no option is selected
  hint: const Text("Select district"),
  items: yourDistrictList.map((district) => DropdownMenuItem(
        value: district.id,
        child: Text(district.name),
      )).toList(),
  onChanged: (newValue) {
    setState(() {
      selectedDistrict = newValue;
    });
  },
)

4. Sync Value with Updated Data

When you refresh your district list from the API, double-check that your current selectedDistrict still exists in the new list. If the district was removed from the API response, you’ll get the error again. Fix this by resetting the selected value when data updates:

// After fetching new district data from API
setState(() {
  yourDistrictList = fetchedDistricts;
  // Check if current selected district is still in the new list
  bool districtExists = yourDistrictList.any((d) => d.id == selectedDistrict);
  if (!districtExists) {
    selectedDistrict = "Select district"; // Or set to null if using hint
  }
});

These steps should cover almost all cases of this error. Since your other features are working, it’s almost certainly a mismatch between your selected value and the menu items—start with verifying if your initial "Select district" value has a matching item in the list!

内容的提问来源于stack exchange,提问作者Mahi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:47:44