Flutter – A Dart Story


Functions, as you can guess from the name, are there to give your app some functionalityFundamentally we’re trying to package bits of code together into a block and to call that block of code repeatedly saving ourselves from typing the same code again and again and again. 

For example if you had a little housekeeping robot his name is Bobi-Brown and he does a number of things, for example he’ll go and get you milk every day but you have to tell him how to do it, you have to tell him to leave the house move right two blocks move up four blocks, then move right two blocks get the milk come back and you have to specify all these instructions to him because he’s a robot and he doesn’t understand conceptual information like “go to the store and get me some milk“. 

We were born to be lazy.

So we can take all these instructions that the robot understands and we can package it into a block of code. A block of code in many programming languages is represented by code that inside a set of curly braces. Once we’ve got all our instructions inside our curly braces we can give that block of code a name, in our case we’ve called it “get milk“. Now every time I need some milk I can tell Bobi-Brown “get milk” and you’ll know what to do because it will find this function called Get Milk and look inside it to see what it should do.

In Dart the way that you would create the function looks like this: 
We’ve got the name of the function which is called getMilk and then we’ve got the curly braces which enclose all of the things that should happen when this function gets called.

In Dart the way that you would create the function looks like this. We’ve got the name of the function which is called Get milk and then we’ve got the curly braces which enclose all of the things that should happen when this function gets called.

This is called a named function.

Now if we dropped everything other than just the core parts namely the parentheses and the curly braces and all the instructions then this would become an anonymous function because it doesn’t have a name.


Variables at the end of the day it’s just a container, a data container. 

var myName = ‘Enrico’;

var is a keyword that says I’m creating a variable then I give that variable a name. 

So in this case it’s called myName and I set what that variables equal to two in this case it’s Enrico all the rest of it such as the semicolons and the equals sign they are all just syntax which is just computer grammar.  Computers also have their own grammar and this grammar differs from programming language to programming language but in part this is what it looks like when you create a new variable.

The keyword var the computer builds a box and then it looks to see what name it should give that box. So this one’s called myName and then it looks at the right side of the equal sign to see what it should put into the box, in this case it’s the word Enrico and the semicolon just says this is the end of my line of code you can close the box now.


For Fast Test Coding i

Data Types

Inside dart when we try to change the variable that’s holding a string of characters, so basically text, and we try to make it hold a number instead, or an integer, it will fall in an error cause he is expecting characters.

We’ve got strings which are essentially a string of characters.

We have int which is short for integer and integers are basically just whole numbers, they can be positive or negative but they can’t have a decimal point.

We have bool which is short for a boolean and it’s a data type that can only hold one of two values either true or false and all of these data types together are known as primitive data types.

So if you wanted a decimal point in Dart you would be working with something called a double, and this is a data type that allows you to have a decimal point and it will hold your data with all those numbers after the decimal point.


Dart also has a data type called dynamic and what this means is that you can create a variable that doesn’t have to have a fixed data type. So if I was creating a variable and I wanted it to be a fixed string data type then I would write String if I wanted to create a variable that had an integer as the data type then I would write int in the beginning instead of var.

Now I could also write dynamic as the key word and now my variable C is dynamic. So if a is only able to hold strings then I can put the value hello inside it but I can’t put the value 1 2 3.

This will generate an error but if I had my variable created as a dynamic type then I can put any sort of data in and I can put one to three and that will be just fine it’ll print out C without a problem I can change C into a string if I wanted to and now when I print the string hello. 

Now gets printed.

This is how dynamic types work and when we created the VAR without giving it a value we essentially created a dynamic type variable.
The important thing here is that if you want to use Dart for its type safety, so ensuring that you don’t mess up the data type and you don’t accidentally put in the wrong thing that you didn’t mean to, then it’s best to create your variables with the data type to start with. 

So if we wanted to create a variable that holds a string, then we would write:

String a = “Hello”;

or whatever value you want.
If we wanted to create a variable that’s an int then we would write:

int b = 1 2 3;

or whatever may be.

And if you’re creating a variable that you don’t yet have a value for, for example the score of the game, as soon as the game starts won’t really have a value. Well then in that case you can create an int and give it a name but not give it a value, but this way further down the line C can never pull a string. It can’t ever change its data type because when we created it we already specified it.
So in terms of best practice I recommend to avoid using var and avoid using dynamic data types unless you actually need it for a reason because it can lead to unexpected things happening in your code and it can lead to crashes down the line.



Leave a Comment

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