refer to
/guides/language/language-tour
text
Today’s article briefly reveals the cool features that Dart language offers. More often, these options are unnecessary for simple applications, but they are a life-saving straw when you want to improve your code with simplicity, clarity and simplicity.
With that in mind, let's go.
Cascade Cascade
Cascades (.., ?..) allows you to perform a series of operations on the same object. This usually saves the steps to create temporary variables and allows you to write more smooth code.
var paint = Paint(); = ; = ; = 5.0; //above block of code when optimized var paint = Paint() ..color = ..strokeCap = ..strokeWidth = 5.0;
Abstract Abstract Class
Use the abstract modifier to define a _abstract abstract class (a class that cannot be instantiated). Abstract classes are very useful for defining interfaces, usually with some implementations.
// This class is declared abstract and thus // can't be instantiated. abstract class AbstractContainer { // Define constructors, fields, methods... void updateChildren(); // Abstract method. }
Factory constructors Factory builders
Use the factory keyword when implementing a constructor that does not always create a new instance of the class.
class Logger { String name; Logger(); factory (Map<String, Object> json) { return Logger(json['name'].toString()); } }
Named constructor
Use named constructors to implement multiple constructors for a class or provide additional clarity:
class Points { final double x; final double y; //unnamed constructor Points(, ); // Named constructor (double x,double y) : x = x, y = y; // Named constructor (double x,double y) : x = x, y = y; }
Mixins Mixture
Mixin is a way to reuse class code in multiple class hierarchies.
To implement implement mixin, create a class that declares that there is no constructor. Unless you want mixin to be used as a regular class, use the mixin keyword instead of the class.
To use mixin, use the with keyword followed by one or more mixin names.
To limit the types that can be used for mixin, use the on keyword to specify the desired superclass.
class Musician {} //creating a mixin mixin Feedback { void boo() { print('boooing'); } void clap() { print('clapping'); } } //only classes that extend or implement the Musician class //can use the mixin Song mixin Song on Musician { void play() { print('-------playing------'); } void stop() { print('....stopping.....'); } } //To use a mixin, use the with keyword followed by one or more mixin names class PerformSong extends Musician with Feedback, Song { //Because PerformSong extends Musician, //PerformSong can mix in Song void awesomeSong() { play(); clap(); } void badSong() { play(); boo(); } } void main() { PerformSong().awesomeSong(); PerformSong().stop(); PerformSong().badSong(); }
Typedefs
Type alias - refers to a concise way of referring to types. Usually used to create custom types that are often used in projects.
typedef IntList = List<int>; List<int> i1=[1,2,3]; // normal way. IntList i2 = [1, 2, 3]; // Same thing but shorter and clearer. //type alias can have type parameters typedef ListMapper<X> = Map<X, List<X>>; Map<String, List<String>> m1 = {}; // normal way. ListMapper<String> m2 = {}; // Same thing but shorter and clearer.
Extension method
The extension method introduced in Dart 2.7 is a way to add functionality to existing libraries and code.
//extension to convert a string to a number extension NumberParsing on String { int customParseInt() { return (this); } double customParseDouble() { return (this); } } void main() { //various ways to use the extension var d = '21'.customParseDouble(); print(d); var i = NumberParsing('20').customParseInt(); print(i); }
Optional position parameters
By wrapping the positional parameters in square brackets, the positional parameters can be made an optional parameter. The optional positional parameter is always the last in the function's parameter list. Unless you provide another default value, their default value is null.
String joinWithCommas(int a, [int? b, int? c, int? d, int e = 100]) { var total = '$a'; if (b != null) total = '$total,$b'; if (c != null) total = '$total,$c'; if (d != null) total = '$total,$d'; total = '$total,$e'; return total; } void main() { var result = joinWithCommas(1, 2); print(result); }
unawaited_futures
When you want to start a Future, the recommended method is to use unawaited
Otherwise, you won't execute without adding async
import 'dart:async'; Future doSomething() { return (Duration(seconds: 5)); } void main() async { //the function is fired and awaited till completion await doSomething(); // Explicitly-ignored //The function is fired and forgotten unawaited(doSomething()); }
The above is the detailed content of 7 cool features of Android development Dart language. For more information about Android development Dart features, please pay attention to my other related articles!