Top 15 Dart features that famed Flutter.

Why Flutter has achieved this much fame in such a short time? Despite launching in 2018, in just 2 years it has gained tremendous community support. Here's one of the reasons, 'DART'.

Why I fell in love with Google Flutter & Dart from the first sight!



Dart is the language that boosted the arsenal of the Flutter. Today we'll see the top 15 features of Dart that made Flutter the 'Ahead Of Time' tool.


1. Sugar Errors: 

As the name suggests, Dart has concentrated on its error giving methods. Many errors in dart are expressed in the form of warnings which lets the user play around with code similar to web development. This feature helps developers to explore their coding by hit and trial methods. And also the exceptions generated consists of the problem description and use of the correct way to get over it.


catcher | Flutter Package


This type of exception can make your debugging easy.


 2. Reusable code:  

Dart can be compiled to Js that can be executed on all the modern web so your 70% of the code can be reused by this feature.

 $ dart2js -O2 -o test.js test.dart
By this command, you can compile your Dart code straight into Js. It also gives you the roadmap of how to debug the Js version app more easily.


3. Ahead Of Time (AOT) : 

Due to this feature, the Dart code can be directly converted into native machine language. Apps build with Flutter are deployed to the App Store with AOT.

Blockquote


4. Unified Layout: 

Dart doesn't use the separate templating for the program and the design like XML or JSX nor it requires visual layouts. It becomes really easy to publish your layouts.


    Container(
         width: 100,
         height: 100,
         child: Center(
            child: Text("Hello Geeks"),
         ),
         decoration: BoxDecoration(
             border: Border.all(color: Colors.blue),
         ),
     ),


This above code can give you a simple square representation just by Dart.

This is the output of the above code.




5. Familiar Syntax:

It doesn't take much effort to get into Dart if you have already leaned languages like Java or C#. So this makes the learning curve of Dart easy and which benefits in adapting to Flutter.

Which one is best Java or Dart? - Web Development - Stopbyte



6. Making better UI: 

With Dart, users can create a smooth rendering of 60fps apps more efficiently than other cross-platform tools. And these features are easily provided by Dart which makes the performance factor of Flutter great. And here's where Dart takes the point over other tools.

    
Showcase - Flutter
Add caption


7. Factory Constructors:

From the developer's point of view, the factory method always provides an efficient solution to many problems. A Factory constructor is a function that returns the instance of the class. 
For example: If you want to cache an instance of the class and return the same when that class's instance is created.

class User { 
  String name;
  User(this.name);
}

class Logger {
  log(String msg) {
    print(msg);      
  }
}

class MyFactories {
  static Logger logger;
  static User user;
  
  factory Logger() {
    return logger;
  }
  
  factory User() {
    return user;
  }
}

main() {
  // instantiate the classes and put the objects in the factory
  var user = new User("Flinstone");
  var logger = new Logger();
  
  // set the objects to the factory.
  MyFactories.logger = logger;
  MyFactories.user = user;
  
  // Then reference the objects from anywhere in the application statically
  MyFactories.logger.log(MyFactories.user.name);
  MyFactories.logger.log(MyFactories.user.name);
}



8. Mixins: 

Mixins are the way of reusing a class's code in multiple class hierarchy. In other words, if you want to use other classes' code but don't want to inherit that classes (basically, a class just can only have 1 superclass), use Mixin.

abstract class Walker {
  // This class is intended to be used as a mixin, and should not be
  // extended directly.
  factory Walker._() => null;

  void walk() {
    print("I'm coding");
  }
}
class Cat extends Mammal with Walker {}



To use mixin we have to use 'with' keyword.


9. Callable classes:

Through this functionality, you can call the class similarly to calling a function. This becomes pretty useful when you are calling a widget class inside a class.

class FindAverage {
  num call(num first, num second) => (first + second) / 2;
}

main(List<String> args) {
  var avg = FindAverage();

  print(avg(24));
  print(avg(2.84.2));
}


10. Dart to executable:

Dart v2.6+ provides the dart2native command which compiles your source code into native executable code or .exe file. Due to this functionality Flutter squares its place in multipurpose functionality platform.

 $ dart2native bin/main.dart -o bin/my_app
By this code, you can convert your .dart to .exe.    

11. @required Annotations:

In Dart by default, all the arguments of a function or a constructor are optional. But it also provides the functionality to mark them required or needed by the @required annotation.

class Test{
  final String x;
  Test({
    @required this.x
  });

  factory Test.initial(){
    return Test(x: "");
  }

}


12. Future:

Dart introduces us to the asynchronous programming concept via Future methods. As the name suggests, it returns a value in the future. You can use methods like .then() to run some specific code after the Future method ends. And you can also convert the asynchronous behavior of Future methods into synchronous by await and async which waits until the Future method gets over.

import 'dart:async';
Future<void> openFile(String fileName) async {
  throw new Exception("boom!");
}
void main() async {
  try
  {
    var result = await openFile("theFile");
    print("success!");
  }
  catch(e) {
    print("Looks like we caught an error: ${e.toString()}");
  }
}


13. Top-Level Functions: 

 One painful part of Java is that everything has to be put into a class. This is impractical if you want to define a few utility methods to use in your Flutter class. But in Dart, you can define functions on the top level that means outside your class. This makes library composition feel more natural.

main() {
  
  //Top-level function
  print('Hello World');
  
}

Here, print is a top-level function.


14. Named Constructors:

You can also give constructors your descriptional names which you wanna like to remember.

    var duck = new Duck.fromJson(someJsonString).


Here, duck in the named constructor for the class Duck.


15. Keywords:

Dart does not use public, private, and protected in a conventional way. Instead, it uses simple keywords like "_" to declare a private variable.

class Text{
  final String _name;
  final int _num;

  Text(this._name, this._num);

}












 







Comments

Post a Comment