So you heard about the Flutter framework and want to know what is it really about. But before that you need to know Dart programming language. Now the language itself is easy to learn and very similar to JavaScript(no pun intended). But there are some new aspects to the language that might surprise you and which must know to comfortably write Flutter code. Google has provided a fairly lengthy guide to get started with Dart which you can find here. I highly recommend you to read it. In these tutorial series I will teach you just enough Dart that you need to know to write Flutter applications with any pain.

Note: This is by no means an in depth tutorial on Dart, we are not covering all the features in the language. You will learn all the essential parts of the language that will be sufficient enough so that you don’t face any barrier from the language side when reading/writing Flutter code.

Hello World

Respect the holly ‘Hello World’ program.

Similar to Java every Dart program must have a main which is its entry point.

To run the program save it in a file named ‘hello_world.dart’ and execute the following command in terminal.

Defining Variables

Just like in JavaScript you can use the var keyword to define variables.

But! unlike JavaScript, in Dart 2, once assigned a type is assigned you can’t reassign a value with new type to the variable. Dart automatically infers the type of data from the right hand side.

You can also define variables by explicitly providing type of data.

If you don’t intend to change the value held by a variable, then declare it with a final or a const.

Difference between final and const is that const variables are compile time constants i.e. const variables must have a value during compile time, for example  const PI = 3.14; , whereas variables that are final can only be assigned once, they need not be assigned during compile time and can be assigned during runtime.

Built In Data Types

Dart provides all the basic data types that you can expect from a modern language.

  • Numbers
  • Strings
  • Booleans
  • Lists
  • Maps

Lists

Declaring a list is very simple, use can simple use square brackets [ ] to define a list. Below are some common operations on a list.

If you want to define a List that is compile time constant i.e. the content of lists are unchangeable then use the const keyword.

Maps

Defining maps is equally straight forward. Use the curly brackets { } to define a map.

You can also define a map using the Map constructor.

If you want to define a Map that is compile time constant i.e. the content of map are unchangeable then use the const keyword.

Never miss a post from TheTechnoCafe


Functions

Functions in Dart are as simple as it can get, somewhat similar to javascript. All you need to do is provide a name, a return type and parameters.

You can even omit the return type and the program will still work.

Here is short hand way of writing single line functions.

Named Parameters

Dart has something called named parameters, when using named parameters you will have to specify the name of parameter while calling the function. To enable named parameters just wrap the parameters in the function with curly brackets { }.

If you don’t provide the name when calling a function with named parameter then the program will crash.

Default Parameter Values

You can give default values to the named parameters thus making them optional when calling the function. In the below example we have given lastName a default value.

Functions are First Class Objects

In Dart you have a lot of flexibility with functions, for example, you can pass a function inside another function.

Here I have defined a function named out that takes in a parameter a function(that has parameter message). Then I have defined a functions named printOutLoud, all it does is print a String in upper case.

Dart also has anonymous functions, so in above example rather than predefining a function(printOutLoud) we can pass an anonymous function.

Another example of anonymous function.

That is it for this tutorial, see you in the next one.

Next Tutorial >> Just enough Dart for Flutter – Tutorial 02 – Control Flow and Exceptions.


Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!

7 Comments

Gabriel · June 13, 2018 at 12:02 pm

Very helpful, thanks

Ewald · June 17, 2018 at 2:18 pm

Excellent! Thank you for a great introduction to Dart, I look forward to the rest of the tutorials.

Dustin · June 18, 2018 at 4:00 pm

> Dart automatically infers the type of data from the left hand side.

I think you mean right hand side.

Just enough Dart for Flutter – Tutorial 02 – Control Flow and Exceptions - TheTechnoCafe · June 18, 2018 at 6:52 pm

[…] Previous Tutorial << Just enough Dart for Flutter – Tutorial 01 – Variables, Types… […]

Flutter Crash Course - 1 - Installation and Overview - TheTechnoCafe · October 23, 2018 at 4:46 pm

[…] Flutter is hybrid mobile application from Google that lets you develop both Android and iOS application from a single codebase. It is not any regular hybrid framework. Unlike other frameworks, Flutter application compiles down to native platform code, resulting in performance and smoothness that has never been seen from a hybrid framework before. Flutter applications are written in Dart language. Dart is a modern, easy to learn Object Oriented language with simple and clean syntax. Dart is very easy language, if you know Java/Javascript you will breeze through Dart code without any problems. Still if you want to learn Dart before getting stated, I have written a tutorial series Just enough Dart for Flutter. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

two × 1 =