top of page

Dart Programming Language


What is Dart?

Dart is a programming language for developing fast, client-side applications that can run across multiple platforms such as the web, mobile, and desktop. Dart, as a programming language, prioritizes development efficiency through capabilities like hot reloading and high-quality app experience across all platforms through many capabilities like layered architecture, AOT (Ahead of Time) compilation, generational garbage collector, etc. Dart supports many features such as type-safety, type inference, and sound null-safety, making an app more robust.


How are Dart and Flutter related?


Fig.1 (Source: flutter.dev)


Fig.1 shows the anatomy of a Flutter app. All the layers shown above make up the Flutter framework. The top two layers (i.e. Dart App and Framework) are Dart-based, i.e. layers that use the Dart programming language. The Dart App layer is a developer’s app code, and the Framework layer provides the high-level APIs (e.g. widgets, gesture detection, text input, etc.) for developers to develop their apps in Dart.


Differences from Java

In my app development course, I used Java to develop Android applications. Contrasting with Java, I have noticed many syntactical differences that I have come to appreciate and make Dart more concise as a programming language.


Public/Private variables:

Dart avoids the usage of keywords like public or private to declare class member variables. In Dart, any member variable starting with an underscore (e.g. _locationName) is considered private. Otherwise, it is public.


New keyword

Compared to Java, Dart avoids using the ‘new’ keyword to instantiate new objects. Below is a simple example of how objects in Java and Dart are instantiated.


Java:

Car car = new Car(‘Toyota’, ‘Prius’);


Dart:

var car = Car(‘Toyota’, ‘Prius’).

There are other syntax differences such as defining generative constructors, getter & setter functions, etc. which make the code more concise to write.


Named parameters

One of Dart’s features that I have come to appreciate is the named function parameters. It is not meant for conciseness but more for ease and convenience.

Java supports positional parameters, i.e. the arguments passed to a function are based on the position of the parameters set during the function definition.

In our car example above, the Car constructor in Java would be defined something like below:


Car(String model, String make) {

this.model = model;

this.make = make;

}


In the above example, model and make parameters of the Car constructor are called positional parameters. They must always be passed in the same order (i.e. the first variable is model, and the 2nd variable is make). While Dart supports positional parameters, it also supports named parameters. In Dart, the above constructor can be defined as:

 

Car({required String model, String make}) {

this.model = model;

this.make = make;

}

 

The curly brackets in the parameter definition make those variables named. The keyword ‘required’ indicates that a parameter is always required. The Car constructor can now be called in various ways:


var car = Car(model: ‘Toyota’, make: ‘Prius’);

var car2 = Car(make: ‘F150’, model: ‘Ford’);

var car3 = Car(model: ‘Honda);

 

The arguments passed to the Car constructor are preceded by the function parameter name and do not have to be passed in the order they were defined. Named parameters become quite handy when instantiating widgets or calling any function that supports tens of parameters.

Recent Posts

See All

Data Storage in Xema

Xema's development is coming along well, with the app now working for multiple users and the LLM model able to respond to a few eczema...

Multi-User Support for Xema

I mentioned previously that I was able to make the Xema app work successfully with Google’s Gemini LLM model. My goal for the app is to...

Comentários


bottom of page