Dart 3 New Features: What Changed in the Biggest Update
In this article
Dart 3, released in May 2023, is the largest language update since Dart 2.0, introducing sound null safety as the default, a new records and patterns system, and a major shift in how the language handles class modifiers. This article breaks down the core changes, what they mean for your code, and how to migrate smoothly.
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Background: Why Dart 3 Matters
Dart 3 is the culmination of a multi-year effort to make the language more expressive, safer, and better suited for large applications. The biggest driver was the Flutter framework, which relies on Dart for its UI. Flutter’s growth exposed the need for better null safety, more concise data handling, and a way to define sealed class hierarchies. Dart 3 delivers on all three.
Before Dart 3, null safety was opt-in, meaning you could write code that allowed null values even after the feature was introduced. Dart 3 makes sound null safety mandatory—every variable is non-nullable by default unless you explicitly allow null. This eliminates a whole class of null reference errors at compile time, a significant reliability win for production code.
Sound Null Safety by Default
In Dart 3, the language no longer supports code that isn’t null safe. This means the ? suffix is now the only way to declare a variable that can hold null. For example:
String name– cannot be null.String? nickname– can be null.
The compiler enforces this everywhere, so you can’t accidentally assign null to a non-nullable variable. This is called “sound” because the compiler can prove that null is impossible in certain contexts, allowing optimizations and preventing runtime crashes. For developers, this means fewer NullPointerException-style bugs and a clearer contract for function arguments and returns.
Records: Lightweight Data Bundles
Records are a new way to group values without creating a class. They are immutable, anonymous, and can hold multiple fields. The syntax uses parentheses:
var point = (x: 3, y: 4);
print(point.x); // 3
Records are perfect for returning multiple values from a function or for temporary data structures. They also support positional fields:
var pair = (1, 'one');
print(pair.$1); // 1
print(pair.$2); // 'one'
This feature reduces the need for small “data holder” classes and makes code more concise. Records are deeply integrated with pattern matching, which we’ll cover next.
Patterns: Destructuring and Matching
Patterns are the most powerful new feature in Dart 3. They allow you to destructure data and match against shapes in a single, readable syntax. You can use patterns in variable declarations, for loops, and switch expressions.
For example, destructuring a record:
var (x, y) = (1, 2);
print(x + y); // 3
Patterns also enable advanced switch statements that can match on types, values, and even nested structures. Consider a sealed class hierarchy:
sealed class Shape {}
class Circle extends Shape { final double radius; }
class Square extends Shape { final double side; }
String describe(Shape shape) => switch (shape) {
Circle(:var radius) => 'Circle with radius $radius',
Square(:var side) => 'Square with side $side',
};
This switch is exhaustive—the compiler knows you’ve covered all subclasses, so you don’t need a default case. This makes code safer and more maintainable.
Class Modifiers: Control Over Inheritance
Dart 3 introduces class modifiers that let you control how classes can be used. The three main ones are:
base– prevents a class from being implemented outside its library.interface– allows implementing but not extending.final– prevents both extending and implementing.
These modifiers give library authors more control over their APIs. For example, a final class cannot be subclassed, which is useful for value types. The sealed modifier we saw earlier is a special case: it restricts subclassing to the same library, enabling exhaustive pattern matching.
Migration and Compatibility
Migrating to Dart 3 is straightforward if you’re already using null safety. The Dart team provides a migration tool that automatically updates your code. For projects still on Dart 2.x, you’ll need to run the tool and manually fix any remaining issues.
One common challenge is dealing with third-party packages that haven’t been updated. The Dart package manager (pub) will warn you if a dependency isn’t null-safe. You can still use such packages, but you’ll lose the soundness guarantees for that part of your code.
Another change: Dart 3 removes support for the dart:mirrors library (reflection) and the dart:ffi API is now the only way to call C code. If you relied on mirrors, you’ll need to find an alternative.
Common Problems and Misconceptions
One misconception is that Dart 3 is just “null safety again.” In reality, records and patterns are equally significant, enabling a more functional style of programming. Another common issue is that developers try to use patterns in contexts where they aren’t allowed, leading to confusing errors. For example, you can’t use a pattern in an assignment (like var (a,b) = (1,2); is fine, but a = 1; b = 2; is not a pattern).
Performance is another area of confusion. Records and patterns are compile-time features—they don’t add runtime overhead. The compiler optimizes them away, so you get the readability without the cost.
Frequently Asked Questions
Is Dart 3 backward compatible with Dart 2?
Not entirely. Dart 3 removes support for unsound null safety and some deprecated APIs. Most code that is already null-safe will work without changes, but you may need to update dependencies and remove any use of removed features like dart:mirrors.
Do I need to migrate my Flutter app to Dart 3?
Yes, if you want to use the latest Flutter versions. Flutter 3.10 and later require Dart 3. The migration is usually automatic, but you should test thoroughly after upgrading.
What are the performance benefits of Dart 3?
The main performance benefit comes from sound null safety, which allows the compiler to eliminate null checks. Records and patterns are compiled to efficient code, so there’s no runtime penalty for using them.
Can I use Dart 3 with my existing IDE?
Yes, all major IDEs (VS Code, IntelliJ, Android Studio) support Dart 3. You just need to update the Dart plugin to the latest version.
Are there any breaking changes in the standard library?
Yes, a few. For example, List is now covariant by default, which can cause issues with type safety. Also, the Future API has some minor changes. Check the official changelog for a full list.
Conclusion
Dart 3 is a major step forward, making the language safer, more expressive, and better suited for modern app development. The combination of sound null safety, records, and patterns will change how you write Dart code, making it more concise and less error-prone. If you haven’t migrated yet, the tools are mature and the benefits are clear—start today.