Gary Gan
Gary Gan

Reputation: 47

changing a string to a int in flutter by using tostring but flutter show an error

i am trying to make a textfield form for the user to input the some numbers in it. then I want to use the number and send it into Duration class. this Duration class can only accept int but my textfield form is text, I cannot change it into int. after some research it say can use to string but flutter tells me the instance member of hourController can't be accessed in an initializer. can any one help. this is my code

final hourController =TextEditingController(); 
//this is the textformfield that I use I name it 
//hourController. here the user will input numbers 
//only. 
int durationhour = hourController;
hourController = durationhour.toString();

Upvotes: 0

Views: 578

Answers (2)

Hemal Moradiya
Hemal Moradiya

Reputation: 2097

this is your text editing controller

final hourController = TextEditingController();

this is how string convert to integer

int durationhour = int.parse(hourController.text);

and this is how you can convert integer to string

hourController.text = durationhour.toString();

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128428

You can use the int.parse(string) method:

var one = int.parse('your string');

Upvotes: 0

Related Questions