DEV Community

Cover image for Dart-lang | Features you didn’t know exists
Kingkor Roy Tirtho
Kingkor Roy Tirtho

Posted on

Dart-lang | Features you didn’t know exists

Dart a single threaded, both AOT (Ahead of time) & JIT (Just in time) compiled, UI programming language that also provides HMR (Hot Reload) out of the box, is one of the growing programming languages nowadays. Its the base language of the famous cross platform UI toolkit Flutter. Dart is known for its extreme syntactical similarities with JavaScript & also being strongly typed “compiled” language. And Yes, it can compile to JavaScript too

Its used by many devs everyday for various Flutter related apps/tools. Chances are you’re one of them. But Flutter being one of the great UI toolkit, Dart keeps giving pain in a Developer’s life. But recently Dart improved so much it is actually making good progress on being one of the easiest & flexible programming language ever next to JavaScript. But unluckily lot of us still don’t know those awesome new features that exist & can be used in the programming language right now if you’re using the latest version

Operator Overloading/Overriding

Have you ever compared two Durations in Dart? You probably used the equality operator == to do that, right? But if you’re coming from other languages like JS/Java/PHP, you’d see something like Duration.isEqual. But in Dart, like most other languages, supports operator overloading

class MyClass {
  final String value;


  bool operator ==(other) {
    this.value == other.value;

void main() {
  final a = MyClass("Hello");
  final b = MyClass("Hello");
  final c = MyClass("Hola")
  print(a == b); //result is true
  print(a == c); //result is false
Enter fullscreen mode Exit fullscreen mode

You can overload every single arithmetic & comparison operator (+, -, *, ≠, ==, >, ≥, <, ≤, [], call()) in Dart using above syntax. More on operator overloading/overriding

Optional Unnamed Parameters

You’re probably familiar with named & unnamed parameters in Dart/Flutter. Named parameters are optional by default but you can make them required by using the required keyword. But did you know you can also make unnamed parameters optional?

void main(){

void example([String? name]){
    return "$name, it's just an owl!";
Enter fullscreen mode Exit fullscreen mode

You can also mix ‘n match with default value optional unnamed parameters & unnamed parameters but you can’t use both named parameters & optional unnamed parameters at the same time

void main(){

// works fine
void what(String firstName, [String? lastName="Jeiber"]){
    return "Mr. $lastName, you're not $firstName";

// doesn't work
// syntax error. Expected ) after
void noway(bool flag, [String? wrong], {bool? isIt}){
    // .....
    // .....

// doesn't work
// syntax error. Expected ) after
void impossible(bool flag, {bool? isIt}, [String? wrong]){
    // .....
    // .....
Enter fullscreen mode Exit fullscreen mode

🔥Super Parameters🔥

Remember this?😵‍💫

class CustomButton extends MaterialButton {
  const CustomButton({
    Key? key,
    required VoidCallback? onPressed,
    VoidCallback? onLongPress,
    ValueChanged<bool>? onHighlightChanged,
    MouseCursor? mouseCursor,
    ButtonTextTheme? textTheme,
    Color? textColor,
    Color? disabledTextColor,
    Color? color,
    Color? disabledColor,
    Color? focusColor,
    Color? hoverColor,
    Color? highlightColor,
    Color? splashColor,
    Brightness? colorBrightness,
    double? elevation,
    double? focusElevation,
    double? hoverElevation,
    double? highlightElevation,
    double? disabledElevation,
    EdgeInsetsGeometry? padding,
    VisualDensity? visualDensity,
    ShapeBorder? shape,
    Clip clipBehavior = Clip.none,
    FocusNode? focusNode,
    bool autofocus = false,
    MaterialTapTargetSize? materialTapTargetSize,
    Duration? animationDuration,
    Widget? child,
  }) : assert(autofocus != null),
       assert(elevation == null || elevation >= 0.0),
       assert(focusElevation == null || focusElevation >= 0.0),
       assert(hoverElevation == null || hoverElevation >= 0.0),
       assert(highlightElevation == null || highlightElevation >= 0.0),
       assert(disabledElevation == null || disabledElevation >= 0.0),
       assert(clipBehavior != null),
         key: key,
         onPressed: onPressed,
         onLongPress: onLongPress,
         onHighlightChanged: onHighlightChanged,
         mouseCursor: mouseCursor,
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
         focusColor: focusColor,
         hoverColor: hoverColor,
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         elevation: elevation,
         focusElevation: focusElevation,
         hoverElevation: hoverElevation,
         highlightElevation: highlightElevation,
         disabledElevation: disabledElevation,
         padding: padding,
         visualDensity: visualDensity,
         shape: shape,
         clipBehavior: clipBehavior,
         focusNode: focusNode,
         autofocus: autofocus,
         materialTapTargetSize: materialTapTargetSize,
         animationDuration: animationDuration,
         child: child,
// ..... other stuff .... //
Enter fullscreen mode Exit fullscreen mode

Not much long ago we had to specify every single constructor parameter in a child class if we wanted/the parent class required to pass them to the super constructor call. It was bloaty & was kind of a shame for the Dart Programming Language

Starting from Dart 2.17 stable we got super parameters. Now we can pass super constructor parameters directly from the child constructor

The above code now looks like this:

class CustomButton extends MaterialButton {
    required super.onPressed,
// ..... other stuff .... //
Enter fullscreen mode Exit fullscreen mode

Finally my 2.5MB HTTP response models can be squeezed down to 200KB

Even though the Dart programming language still have a lot of flaws & lot features not yet implemented but its evolving. It was super hard to work with but now has become a lot more flexible. I hope a lot of new features will be added time by time making the Language more mature + modern

Follow me on

Top comments (0)