How to reset value in Flutter DropdownButtonFormField

4,001

Solution 1

You can use a GlobalKey<FormFieldState>.


class SomeWidget extends StatelessWidget {
  final GlobalKey<FormFieldState> _key;

  @override
  Widget build() {
    return DropdownButtonFormField(
      key: _key,
      //...
    )
  }

  reset() {
    _key.currentState.reset();
  }
}

https://dartpad.dev/04247868fa2e86c5f417532486b48870

Solution 2

class SomeWidget extends StatelessWidget {
  final GlobalKey<FormFieldState> _key = GlobalKey<FormFieldState>();

  @override
  Widget build() {
    return DropdownButtonFormField(
      key: _key,
      //...
    )
  }

  reset() {
    _key.currentState.reset();
  }
}
Share:
4,001
Maroshb
Author by

Maroshb

Updated on December 19, 2022

Comments

  • Maroshb
    Maroshb over 1 year

    I want to reset value at DropdownButtonFormField, value is changed to null, but DropdownButtonFormField doesn't change. Where is the problem? If I have used DropdownButton it was changing value properly, value was cleared. I need to use DropdownButtonFormField.

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
    
      @override
      MyAppState createState() => MyAppState();
    }
    
    
    class MyAppState extends State<MyApp> {
    
      String abc;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center (
              child: Column(
                children: <Widget>[DropdownButtonFormField(
                  hint: Text('select value'),
                  value: abc,
                  items: <String>['A', 'B', 'C', 'D'].map((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
                  onChanged: (String newValue) {
                    setState(() {
                      abc = newValue;
                    });
                  },
               ),
                Text("value is $abc"),
              ],
              ),
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: (){
                setState((){
                  abc = null;
                });
             },
             tooltip: 'Reset',
             child: Icon(Icons.clear),
           )        
           ),
        );
      }
    }
    
    • Abhishek Ghaskata
      Abhishek Ghaskata almost 4 years
      give the first value rather than null. In your case, that is "A" or assing " "
    • Maroshb
      Maroshb almost 4 years
      It is not what I need... I know that there can be empty item.. It have to work that way, with DropdownButton it is possible... there isn't problem null value , on button press it cannot set any value from items.
    • Abhishek Ghaskata
      Abhishek Ghaskata almost 4 years
      try DropdownButton widget
    • Maroshb
      Maroshb almost 4 years
      with DropDownButton it works as wrote .. but it has not some features as DropDownButtonFormField
    • Krunal Darji
      Krunal Darji almost 4 years
      @Maroshb you found any solution with DropdownButtonFormField?
    • Maroshb
      Maroshb almost 4 years
      No, I have used DropDownButton for now