i want to get int data entered in the TextField() in flutter, i using TextEditingController:
TextEditingController _section_id = new TextEditingController();
and using this controller in it:
TextField(controller: _section_id,
keyboardType: TextInputType.number,)
but now, how can i get int data? i try this way by
Repository().placeAddApiProvider(_section_id.text)
but its for just string, and try cast to int,
Repository().placeAddApiProvider(_section_id.text as int)
but it is not work show me this error:
Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
E/flutter ( 6950): #0 AddPlaceState.build.<anonymous closure> (package:mosul/src/ui/users/add_place.dart:93:50)
E/flutter ( 6950): <asynchronous suspension>
E/flutter ( 6950): #1 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
E/flutter ( 6950): #2 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
E/flutter ( 6950): #3 GestureRecognizer.invokeCallback (package:flutter/src/gestu...
Thank you
TextEditingController class Null safety. A controller for an editable text field. Whenever the user modifies a text field with an associated TextEditingController, the text field updates value and the controller notifies its listeners.
Second, remember to dispose of the TextEditingController inside dispose() when it is no longer needed. This will ensure we discard any resources used by the object. There is no need to dispose of it while in use. Remember: the controller is not there to notify listeners of the changes inside the text input field.
when we want to get an integer from TextEditingController do like this,
int var =int.parse(_section_id.text);
You cannot cast a String
to int
because they do not inherit from the same parent class. You need to parse it instead.
Repository().placeAddApiProvider(int.parse(_section_id.text))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With