skip to Main Content

This is my code.
I declare like this :

var selectedEquipment;

this is my code to add default value in dropdown

if(!isNew) {
      setState(() {
        selectedEquipment = intervention['equipment'];
      });
    }

And in my DropdownButton

DropdownButtonFormField(
                  value: selectedEquipment,
                  icon: const Icon(Icons.arrow_drop_down_circle_outlined),
                  iconSize: 24,
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Equipment',
                    labelStyle: TextStyle(color: cGrey, fontSize: 14),
                    floatingLabelStyle: TextStyle(color: cPrimary, fontSize: 18, fontWeight: FontWeight.w700),
                  ),
                  items: equipmentList.map((item){
                    return DropdownMenuItem(
                      value: item,
                      child: Text(
                        item['label'],
                        style: const TextStyle(color: Colors.black),
                      ),
                    );
                  }).toList(), 
                  onChanged: (newValue) {
                    setState(() {
                      selectedEquipment = newValue;
                      isEquipment = true;
                    });
                    // selectEquipment(newValue);
                  },
),

When I run, I have this error

enter image description here

Exception has occurred.
_AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: line 1606 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1': There should be exactly one item with [DropdownButton]'s value: {id: 27, label: tesy}. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)

2

Answers


  1. Chosen as BEST ANSWER

    I solved this issu. I added equatable and It work fine now

    class Equipment {
      late final int id;
      late final String label;
      
      Equipment({required this.id, required this.label});
    
      @override
      bool operator ==(Object other) =>
          identical(this, other) ||
          other is Equipment && runtimeType == other.runtimeType && id == other.id && label == other.label;
    
      @override
      int get hashCode => id.hashCode ^ label.hashCode;
    }
    

  2. New Answer

    item returns a List type and value takes string type.
    Change value: item, into value: item['id']

    DropdownMenuItem(
          value: item['id'], 
          child: Text(
            item['label'],
            style: const TextStyle(color: Colors.black),
          ),
        );
    

    Old Reply(Not Working):

    When your app/page initialize the value of selectedEquipment is empty.
    For this reason it is showing the There should be exactly one item with [DropdownButton]'s value.
    Try to initialize the value of selectedEquipment in initState

    var selectedEquipment;
    setEquipmentValue(bool isNew){
        if(!isNew) {
            setState(() {
                selectedEquipment = intervention['equipment'];
            });
        }
    }
    @override
      void initState() {
        setEquipmentValue();
        super.initState();
      }
    

    or

    var selectedEquipment;
    @override
      void initState() {
        if(!isNew) {
          setState(() {
            selectedEquipment = intervention['equipment'];
          });
        }
        super.initState();
      }
    

    or you can give a value to selectedEquipment when declaring for first time

    var selectedEquipment = intervention['equipment'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search