7 Cool Features of Dart Language
Mixins, Cascade notation, and more
Today’s article is a short revelation of the cool features that the dart language offers. More often these options are not necessary for simple apps but are a lifesaver when you want to improve your code by making it simple, clear, and concise.
With that in mind let’s go.
Cascade notation
Cascades (..
, ?..
) allow you to make a sequence of operations on the same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.
Abstract classes
Use the abstract
modifier to define an abstract class (a class that can’t be instantiated). Abstract classes are useful for defining interfaces, often with some implementation.
Factory constructors
Use the factory
keyword when implementing a constructor that doesn’t always create a new instance of its class.
Named constructors
Use named constructors to implement multiple constructors for a class or to provide extra clarity:
Mixins
Mixins are a way of reusing a class’s code in multiple class hierarchies.
To implement a mixin, create a class that declares no constructors. Unless you want your mixin to be usable as a regular class, use the mixin
keyword instead of class
.
To use a mixin, use the with
keyword followed by one or more mixin names.
To restrict the types that can use a mixin use the on
keyword to specify the required superclass.
Typedefs
A type alias — is a concise way to refer to a type. Commonly used to create a custom type that is used a lot in the project.
Extension methods
Extension methods, introduced in Dart 2.7, are a way to add functionality to existing libraries and code.
Optional positional parameters
You can make positional parameters optional by wrapping them in brackets. Optional positional parameters are always last in a function’s parameter list. Their default value is null unless you provide another default value.
unawaited_futures
When you want to start a fire-and-forget Future
, the recommended way is to use unawaited
If you come across another cool feature, please let me know so that I can top it up on this list.
That’s all for today.