Data type conversion in Dart or Flutter
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
In Dart and Flutter, you can perform data type conversions using various methods and constructors provided by the language. Here are some commonly used data type conversions:
String to int:
String stringValue = '123';
int intValue = int.parse(stringValue);
String to double:
String stringValue = '3.14';
double doubleValue = double.parse(stringValue);
int to String:
int intValue = 42;
String stringValue = intValue.toString();
double to String:
double doubleValue = 3.14;
String stringValue = doubleValue.toString();
String to boolean:
String stringValue = 'true';
bool boolValue = stringValue.toLowerCase() == 'true';
boolean to String:
bool boolValue = true;
String stringValue = boolValue.toString();
String to DateTime:
String dateString = '2023-06-23';
DateTime dateTimeValue = DateTime.parse(dateString);
DateTime to String:
DateTime dateTimeValue = DateTime.now();
String dateString = dateTimeValue.toString();
Object to a specific type:
Object obj = 'Hello';
String stringValue = obj as String;