Data type conversion in Dart or Flutter

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;
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x