Home > OS >  Exception has occurred. FormatException (FormatException: Invalid double )
Exception has occurred. FormatException (FormatException: Invalid double )

Time:11-08

class Mytextlist extends StatelessWidget {
     final Function adddata;
       Mytextlist(this.adddata);
      TextEditingController titleController = TextEditingController();
    TextEditingController valueController = TextEditingController();
    @override
   Widget build(BuildContext context) {
     return Container(
    decoration: BoxDecoration(
        border: Border.all(
      color: Colors.black,
    )),
    child: Column(
      children: [
        Card(
            child: Column(children: <Widget>[
          TextField(
            decoration: const InputDecoration(
              labelText: "title",
            ),
            controller: titleController,
          ),
        ])),
        Card(
            child: Column(children: <Widget>[
          TextField(
            decoration: const InputDecoration(labelText: "expenses"),
            controller: valueController,
          ),
        ])),
        ElevatedButton(
            onPressed: adddata(
                titleController.text,
                double.parse(
                  (valueController.text),
                )),
            child: const Text("ADD Transaction")),
      ],
    ));

} }

//here i want to pass the value that was entered in the Textfield , my function adddata which will invoked while pressing button,which accepts string and double value, titlecontroller.text coverts value to string but double.parse is not converting the data into double , and the application is throwing error at format exception

CodePudding user response:

I don't know what value you trying to parse but I assume that you are using comma instead of point

14,54 - invalid format

but

14.54 - valid format

Just replace comma with point before parsing

CodePudding user response:

Please provide details of your value which you are passing. If you are passing it with comma you have to remove those commas before passing it to double.

ElevatedButton(
    onPressed: (){
      // Assume your valueController.text = 11,000
      var inputValue = valueController.text.replace(",", ""); 
      adddata(titleController.text, double.parse(inputValue)),
    }
    child: const Text("ADD Transaction")
),

  • Related