Search code examples
flutterflutter-textformfieldflutter-textinputfield

Flutter: How to store value from flutter textfield as int?


how to store a value from textfield as int so I can use it with operator for calculation?


Solution

  • If you have something like this :

    TextEditingController textFieldValue= TextEditingController();
    
    //[...]
    
    
    TextFormField(
        controller: textFieldValue,
        decoration: const InputDecoration(
            labelText: "Enter an integer :"
        ),
        validator: (value) {
            if (value == null || value.isEmpty) {
                return 'Enter valid data';
            }
            return null;
        },
    ),
    

    you can use the value with :

    var integerFromTextfield = int.parse(textFieldValue.text);