Conversion of values into flutter

Asked

Viewed 3,211 times

-2

I need to take a figure that comes from a field like text. However, this field only receives numbers.

Text field:

Container(
              child: CupertinoTextField(
                textCapitalization: TextCapitalization.none,
                placeholder: 'Digite um numero',
                decoration: BoxDecoration(
                  border: Border.all(
                    width: 1.0,
                    color: Colors.deepOrangeAccent,
                  ),
                  borderRadius: BorderRadius.circular(25.0),
                ),
                maxLines: null,
                keyboardType: TextInputType.number,
                controller: controllerTextnum,
              ),
              padding: EdgeInsets.fromLTRB(17.0, 30.0, 25.0, 1.0),
            ),

Knob:

 Container(
              child: CupertinoButton(
                color: Colors.deepOrangeAccent,
                child: Text("Mostrar número digitado",
                    style: TextStyle(
                      fontSize: 20.0, color: Colors.white,
                    )),
                onPressed: (){
                  alertNum(context);
                },
              ),
              padding: EdgeInsets.fromLTRB(17.0, 30.0, 25.0, 1.0),
            ),

Alert that shows what was typed in the field.

void alertNum (BuildContext context){
  var alert = CupertinoAlertDialog(
    title: Text("O NúMERO Digitado foi:"),
    content: Text(controllerTextnum.text),
    actions: <Widget>[
      CupertinoDialogAction(
        child: Text("Fechar"),
        onPressed: (){
          Navigator.of(context).pop();
       
        },
      ),
    ],
  );
  showDialog(context: context,
  builder: (BuildContext context){
    return alert;
  }
  );
}

I’d like to take that amount that comes from CupertinoTextField and for example multiply by 2.

He had done a function that "played" him in a Dynamic variable, however, it did not work.

  • This is something very simple, Voce has the methods of conversation, int.parse(string) and double.parse(string) and all this is in doc, if you put in google flutter convertrto to int or to double, will appear various post. You have to do more research before you ask.

2 answers

0

Use the method parse:

double novoValor = double.parse(controllerTextnum.text);

As stated in the documentation, it will play an exception if the given string is not valid as double. To prepare for such a situation, involve the control in a block try / catch, or use the following notation:

double.tryParse(controllerTextnum.text) ?? (ValorASerEscolhidoCasoFalhaNoParse)

0

That question is more related to Dart than Flutter.

But you can use the methods parse and tryParse, present in the num, int and double. That convert a String at a numerical value.

Example:

void main() {
  String text = '42';
  String text2 = '42.5';

  //int numero = int.parse(text2); //Erro
  int intTryParse = int.tryParse(text2);
  print('Se deu erro no parse, retorna null: $intTryParse');

  int intParse = int.parse(text);
  print('Valor $intParse, x2: ${intParse * 2}');

  double doubleParse = double.parse(text2);
  print('Valor $doubleParse, x2: ${doubleParse * 2}');

  num numParse = num.parse(text2);
  print('Valor $numParse, x2: ${numParse * 2}');
} 

Test here on Dartpad.

Note that if you try to convert a double to int using only the parse, an error will be triggered, already with the tryParse the error is not triggered, but a null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.