1
votes

I have a dropDownFormField where I have a doprDownButton items. Every other items inside that drop down works except one that says "Thrice a week".

Tried putting a condition before setting the value of the dropdown didn't work

Expanded(
  flex: 1,
  child: Container(
    width: 180.0,
    child: DropdownButtonFormField(
      decoration: InputDecoration(
          filled: true,
          labelText: "Frequency",
          border: OutlineInputBorder(),
          fillColor: Colors.black12),
      validator: (val) {
        if (val == null ) {
          return "Select the units";
        } else {
          return null;
        }
      },
      items: dummyData.frequency
          .map((value) => DropdownMenuItem(
                child: Text(
                  value,
                ),
                value: value,
              ))
          .toList(),
      onChanged: (selectedFrequency) {
        setState(() {
          selectedFrequencyItem = selectedFrequency;
        });
      },
      value: selectedFrequencyItem != null ? selectedFrequencyItem : null,
    ),
  ),
),

════════ (2) Exception caught by widgets library ═══════════════════════════════════════════════════ 'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true. User-created ancestor of the error-causing widget was:

1
Is there a possible that you got null or empty dummyData.frequency? This is what the error indicated on items.Tokenyet
I just checked I have 21 list of strings which display properly but only one item is giving bugs when selected this item => "Thrice a week".Dawith305
I have another item that says "Thrice a day" and I change the next one to Thrices a week and it seems to workDawith305

1 Answers

0
votes

Items of DropdownButtonFormField must be unique, accidentally you had two "Thrice a week" strings in the "frequency" list.

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: SafeArea(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> frequency = [
    'Immediately',
    'Once a day',
    "Twice a day",
    "Thrice a day",
    "Four times a day",
    "Every 2 hours",
    "Every 3 hours",
    "Every 4 hours",
    "Every 6 hours",
    "Every 8 hours",
    "Every 12 hours",
    "On alternative days",
    "Twice a week",
    "Thrice a week",
    "Every 2 weeks",
    "Every 3 weeks",
    "Once a month",
    "Five times a day",
    "Four days a week",
    "Five days a week",
    "Six days a week",
  ];

  String selectedFrequencyItem;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 180,
      child: DropdownButtonFormField(
        decoration: InputDecoration(
          filled: true,
          labelText: "Frequency",
          border: OutlineInputBorder(),
          fillColor: Colors.black12,
        ),
        items: frequency
            .map((value) => DropdownMenuItem(
                  child: Text(
                    value,
                    style: TextStyle(fontSize: 12),
                  ),
                  value: value,
                ))
            .toList(),
        onChanged: (selectedFrequency) {
          setState(() {
            selectedFrequencyItem = selectedFrequency;
          });
        },
        value: selectedFrequencyItem,
      ),
    );
  }
}