Different types of typecasting available in dart?

Dart is a type-safe programming language, that combines static type check and runtime check and a single run, it is also called sound typing.

String to int

void main() {
  String a = "1";
  
  int c = int.parse(a);
  print(c); //1
}


String to double

void main() {
  String a = "123.6";
  
  double c = double.parse(a);
  print(c);
}

Int to string

void main() {
  int a = 24;
  
  
  print(a.toString());
}

Double to string

void main() {
  double a = 24.5;

  print(a.toString());
}

Int to double

void main() {
 int x = 11;

double y = x.toDouble();
  print(y);
}

Double to int

void main() {
 double x = 11.0;

double y = x.toDouble();
  print(y);
}

Map to list

void main() {
 List list = [];
  Map<int, String> map = {0: "one", 1: "two", 2: "three"};
 list = map.entries.map((entry) => "${entry.value}").toList();
print(list);
}

List to map

void main() {
  List<String> listString = ['one', 'two', 'three'];
  Map<int, String> map = listString.asMap();
  print(map);
}

Type checker

is ,as is!

Dart “as” operator is used for explicit typecast.

Dart “is” operator is used for type check for any type of variable return true if an object is its type.

Dart “is!” operator is used for type check for any type of variable return true if object is not specified type.

Conclusion

in this post we learnt about typecasting in dart , Happy coding.

Leave a Comment