Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get int data from TextEditingController in flutter

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

like image 745
Osama Mohammed Avatar asked May 19 '19 11:05

Osama Mohammed


People also ask

What is the use of TextEditingController in flutter?

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.

Should I dispose TextEditingController?

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.


2 Answers

when we want to get an integer from TextEditingController do like this,

 int var =int.parse(_section_id.text);
like image 155
Nikolas Toni Avatar answered Nov 16 '22 20:11

Nikolas Toni


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))
like image 41
Michael Yuwono Avatar answered Nov 16 '22 19:11

Michael Yuwono