As we all know Double data type is like parent of Float data type. In current programming arena most of developers use Double data type as place of Float data type. Dart gives us a inbuilt function named as double.parse(String) which is used to convert String numbered value to Double so the user can perform any type of mathematical operation on that value. So in this tutorial we would Flutter Dart Convert String to Double Float Android iOS Example Tutorial.
Contents in this project Flutter Dart Convert String to Double Float 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 here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main MyApp extends StatelessWidget. This is our main Parent class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating a String variable named as value1 with some random numbered string value.
1 |
String value1 = '12345678'; |
5. Creating another variable named as value2 of Double data type. We would use this variable to store the converted value.
1 |
double value2; |
6. Creating a function named as changeType(). In this function we would use the double.parse() method to convert the String to double float value and then store the value in value2 variable. We would print the converted value on Terminal screen after done conversion.
1 2 3 4 5 |
changeType() { value2 = double.parse(value1); print(value2); } |
7. Creating Widget Build area -> Material App widget -> Scaffold widget -> Center widget -> Column widget -> Raised Button widget. We would call the above changeType() function on Button click event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => changeType(), child: Text('Convert String to Double on Click'), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ])))); } |
8. 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 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { String value1 = '12345678'; double value2; changeType() { value2 = double.parse(value1); print(value2); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => changeType(), child: Text('Convert String to Double on Click'), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ])))); } } |
Screenshots:
