0
votes

I have a class that called with Satuan, now i want to set the value to it then use it in my dropdown as selected value . Here is what i do

 Satuan selectedUom;
 Future<String> get_itemuom() async {
    var res = await widget.itemCategoryRepository.get_uom();
    setState(() {
      res.forEach((element) {
        if (element.uomId == widget.item.uomId) {
          selectedUom = Satuan(
              uomId: element.uomId,
              uomName: element.uomId,
              flagDecimal: element.flagDecimal);
        }
      });
      itemsuom = res
          .map(
            (uom) => DropdownMenuItem(
              child: Text(uom.uomName),
              value: uom,
            ),
          )
          .toList();
    });
    return "Sucess";
  }

here is my dropdown

   Widget satuan_field() {
return (itemsuom.length < 1)
    ? Center(
        child: CircularProgressIndicator(),
      )
    : Padding(
        padding: EdgeInsets.fromLTRB(20, 5, 20, 10),
        child: Container(
          color: Color(0xfff5f5f5),
          child: new DropdownButtonFormField<Satuan>(
            validator: (value) {
              if (value == null) {
                return 'Wajib di isi';
              }
              return null;
            },
            style: TextStyle(color: ColorThemes().textColor),
            decoration: InputDecoration(
                enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(25.0),
                  borderSide: BorderSide(
                    color: ColorThemes().mainColor,
                    width: 2.0,
                  ),
                ),
                border: OutlineInputBorder(),
                labelText: 'Satuan',
                prefixIcon: Icon(Icons.card_membership,
                    color: ColorThemes().secondaryColor),
                labelStyle: TextStyle(
                    fontSize: ColorThemes().default_text_size,
                    color: ColorThemes().secondaryColor)),
            items: itemsuom,
            value: selectedUom,
            onChanged: (value) {
              setState(() {
                _mySelectionuom = value.uomId;
                isInputDecimal = value.flagDecimal;
                _itemStock.text = "";
              });
            },
          ),
        ),
      );

}

when i run it i get this error

I/flutter ( 6652): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 6652): The following assertion was thrown building EditScreen(dirty, state: _EditScreenState#82e0b): I/flutter ( 6652): There should be exactly one item with [DropdownButton]'s value: Instance of 'Satuan'. I/flutter ( 6652): Either zero or 2 or more [DropdownMenuItem]s were detected with the same value I/flutter ( 6652): 'package:flutter/src/material/dropdown.dart': I/flutter ( 6652): Failed assertion: line 1478 pos 15: 'items == null || items.isEmpty || value == null || I/flutter ( 6652):
items.where((DropdownMenuItem item) { I/flutter ( 6652):
return item.value == value; I/flutter ( 6652): }).length == 1'

How can i fix it ? thanks in advance

1

1 Answers

1
votes

This line is returning duplicate items, so the dropdown can't decide which one is which.

var res = await widget.itemCategoryRepository.get_uom();

Do you read data using FutureBuilder?