Skip to content

DropdownMenu's value can't be reset/cleared #165877

Closed

Description

@ndonkoHenri

Steps to reproduce

Run the code, and click the reset button to clear the content of the dropdowns.

Expected results

Clicking on the reset button resets both dropdowns. DropdownMenu doesnt.

Actual results

Only the DropdownButton gets reset.

Code sample

Code sample
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: DropdownExample());
  }
}

class DropdownExample extends StatefulWidget {
  @override
  State<DropdownExample> createState() => _DropdownExampleState();
}

class _DropdownExampleState extends State<DropdownExample> {
  final List<String> items = ['Apple', 'Banana', 'Cherry'];
  String? selectedMenuItem = 'Apple';
  String? selectedButtonItem = "Apple";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Dropdown Demo')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            DropdownMenu<String>(
              initialSelection: selectedMenuItem,
              onSelected: (value) => setState(() => selectedMenuItem = value),
              dropdownMenuEntries:
                  items
                      .map((e) => DropdownMenuEntry<String>(value: e, label: e))
                      .toList(),
            ),
            SizedBox(height: 20),
            DropdownButton<String>(
              hint: Text('Select an item'),
              value: selectedButtonItem,
              onChanged: (value) => setState(() => selectedButtonItem = value),
              items:
                  items
                      .map(
                        (e) =>
                            DropdownMenuItem<String>(value: e, child: Text(e)),
                      )
                      .toList(),
            ),
            SizedBox(height: 40),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  selectedMenuItem = null;
                  selectedButtonItem = null;
                });
              },
              child: Text('Reset Dropdowns'),
            ),
          ],
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output

DartPad with Flutter 3.29.2 and Dart 3.7.2

Activity

  1. maheshj01 commented on Mar 26, 2025

    @maheshj01
    Member

    @ndonkoHenri you should try using the texteditingController to reset the field, selectedMenuItem in your field is being used for initialSelection updating it won't change the selectedItem value

    code sample
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(home: DropdownExample());
      }
    }
    
    class DropdownExample extends StatefulWidget {
      @override
      State<DropdownExample> createState() => _DropdownExampleState();
    }
    
    class _DropdownExampleState extends State<DropdownExample> {
      final List<String> items = ['Apple', 'Banana', 'Cherry'];
      String? selectedMenuItem = 'Apple';
      String? selectedButtonItem = "Apple";
      final TextEditingController controller = TextEditingController();
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Dropdown Demo')),
          body: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                DropdownMenu<String>(
                  controller: controller,
                  initialSelection: selectedMenuItem,
                  onSelected: (value) => setState(() => selectedMenuItem = value),
                  dropdownMenuEntries: items
                      .map((e) => DropdownMenuEntry<String>(value: e, label: e))
                      .toList(),
                ),
                SizedBox(height: 20),
                DropdownButton<String>(
                  hint: Text('Select an item'),
                  value: selectedButtonItem,
                  onChanged: (value) => setState(() => selectedButtonItem = value),
                  items: items
                      .map(
                        (e) => DropdownMenuItem<String>(value: e, child: Text(e)),
                      )
                      .toList(),
                ),
                SizedBox(height: 40),
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      controller.clear();
                      selectedMenuItem = null;
                      selectedButtonItem = null;
                    });
                  },
                  child: Text('Reset Dropdowns'),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    Closing as this is working as intended, Incase you disagree please feel free to write in the comments and we will reopen it.

  2. added
    r: invalidIssue is closed as not valid
    and removed
    in triagePresently being triaged by the triage team
    on Mar 26, 2025
  3. github-actions commented on Apr 9, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  4. locked as resolved and limited conversation to collaborators on Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: invalidIssue is closed as not valid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions