Junior — Middle
What has changed in Dart 2.0 regarding automatic type conversion and why was it abandoned?
sobes.tech AI
Answer from AI
In Dart 2.0, automatic type conversions (implicit downcasts) were abandoned to improve type safety and performance. Previously, the compiler automatically cast types, which could lead to runtime errors if types did not match.
Now, all type conversions must be explicit, allowing errors to be detected at compile time and making the code more predictable and reliable.
Example:
Object obj = "string";
String str = obj; // Compilation error in Dart 2.0
String str = obj as String; // Correct, explicit cast
Thus, abandoning automatic casting has improved code quality and type safety.