Flutter comes with inbuilt string methods and one of them is String toString() function which is used in Flutter to cast Int Variable to String type. The String toString() function returns integer into String format so we can easily use the number directly as String data type. So in this tutorial we would Flutter Convert Int Integer Variable to String Data Type in Dart on button click on app runtime Android iOS example tutorial.
Contents in this project Flutter Convert Int Integer Variable to String Data Type Dart Android iOS Example Tutorial:
1. Import material.dart package in your app’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and call our main MyApp class here.
1 |
void main() => runApp(MyApp()); |
3. Create our main MyApp class extends with State less widget and here we would call the Convert() class in body section.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Convert() ) ) ); } } |
4. Create Convert class extends with StatefulWidget. In this class we would call the ConvertWidget class using createState() method. Which would enable the State management in given class tree.
1 2 3 4 5 |
class Convert extends StatefulWidget { ConvertWidget createState() => ConvertWidget(); } |
5. Create our child class named as ConvertWidget extends with State.
1 2 3 4 |
class ConvertWidget extends State { } |
6. In ConvertWidget class we would make a Integer variable named as value with some random value and a String variable named as holder. At this point we would not assign any value to String variable.
1 2 |
int value = 1234567890 ; String holder ; |
7. Create a function named as changeType(). In this function we would use the setState() method of flutter to update runtime and convert the Integer variable as String using String toString() variable.
1 2 3 4 5 6 7 8 9 |
changeType(){ setState(() { //Converting Int value to String. holder = value.toString(); }); } |
8. Create widget build area in ConvertWidget class and here we would make a Text widget and 1 Raised Button widget. We would use the Text widget to display the converted number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( padding: EdgeInsets.fromLTRB(0, 10, 0, 10), child: Text('$value', style: TextStyle(fontSize: 22),)), RaisedButton( onPressed: () => changeType(), child: Text('Click Here To Convert Int Type to String on Button Click'), textColor: Colors.white, color: Colors.red, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), ) ])) ); } |
9. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Convert() ) ) ); } } class Convert extends StatefulWidget { ConvertWidget createState() => ConvertWidget(); } class ConvertWidget extends State { int value = 1234567890 ; String holder ; changeType(){ setState(() { //Converting Int value to String. holder = value.toString(); }); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( padding: EdgeInsets.fromLTRB(0, 10, 0, 10), child: Text('$value', style: TextStyle(fontSize: 22),)), RaisedButton( onPressed: () => changeType(), child: Text('Click Here To Convert Int Type to String on Button Click'), textColor: Colors.white, color: Colors.red, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), ) ])) ); } } |
Screenshots:
