Expressions, Variables & Constants in Dart

fluttertanzania

Flutter Tanzania

Posted on October 2, 2023

Expressions, Variables & Constants in Dart

In our first article we focused on installation of dart and wrote our first dart "Hello Word".

Let's start writing code, in this article we will focus on Variable and expressions in dart. Let's go 🥳

Comments in dart

Comment in programming are non executable line's of code which describes what a code is doing.

Writing comment's in your program describes what was your intention to write a certain block of code and what does that code solving. But also in the future more likely other programmers will be using your code, by commenting it will be easier for them to understand your code.

Comment's in dart can be single line or multiple line comments, here's an example of a single line comment.

// This is a comment, it will never be executed
Enter fullscreen mode Exit fullscreen mode

Now let's see how we can write multiple line comments

/* This is a comment also,
but it's to long
let's break it */
Enter fullscreen mode Exit fullscreen mode

In multiple line's comment the start is denoted by /* and the end is denoted by */.

You can write comment in may ways in Dart but to make it simple we'll stop here in comments.

Printing output
When writing a program there's always a point you want to see an output of a certain code. In dart we use print() function. And this display an out put on the console.

Adding print statements into your code is an easy way to monitor what’s happening at a particular point in your code.

Statements and expressions
In programming you will often come across these two words so first let's understand them.

Statements
A statement is a command, something you tell the computer to do, in dart all simple statements end up with a semicolons, a good example is a print statement.

print('Hello, Dart world');
Enter fullscreen mode Exit fullscreen mode

Semicolons identifies an end of a statement.

In addition to simple statements, Dart also has complex statements and code blocks that use curly braces, but there’s no need to add semicolons after the braces.

One example of a complex statement is the if statement:

if (condition) {
  // code block
}
Enter fullscreen mode Exit fullscreen mode

We will learn more about if statement's in Control Flow section.

Expressions
Unlike statements Expressions does not do something, it is something. Expression is a value or something that can be calculated to a value.

Here are some few examples of expressions.

36
1 + 2
'Hello Dart'
firstName
Enter fullscreen mode Exit fullscreen mode

Values can be numbers, text, or any other type. They can also be a variable example firstName who's value is not known during run time.

Arithmetic operations

Now let's learn arithmetic operations that dart offers. Let's start with simple operations that we're all familiar with.

  • Add +
  • Subtract -
  • Multiply *
  • Divide /

For example

1 + 2
5 - 2
9 * 2
12 / 4
Enter fullscreen mode Exit fullscreen mode

Keep in mind that all of these operations are expressions because we expect value from them.

Decimal numbers
Most of programming languages uses integer division by default if you have the following calculation (22 / 7), you will expect the result to be 3, However, Dart gives you the standard decimal answer which will be (3.142857142857143).

But in dart if you would want to perform integer division, then you could use the ~/ operator:

22 ~/ 7
Enter fullscreen mode Exit fullscreen mode

This produces a result of 3.

The ~/ operator is officially called the truncating division operator when applied to numbers.

Euclidean modulo operation
There's a point where you want to divide a number and get the remainder, in dart we use %, which will always return a remainder of a given division calculation.

Example:

10 \ 3
Enter fullscreen mode Exit fullscreen mode

The answer will be 1 which is the remainder.

Math functions
As you become more pro in Dart you will in a situation you want to perform a complex operation, Dart can help you with that by providing you a math function.

To use these math functions, add the following import to the top of your file:

import 'dart:math';
Enter fullscreen mode Exit fullscreen mode

dart:math is one of Dart’s core libraries. Adding the import statement tells the compiler that you want to use something from that library.

By importing the math library you can now use math functions from the library, for example sin(), cos() and more.

Variables

In the simplest form a variable is a name that you give it a value and tell a computer to remember it so you can use it later. The name carries with it an associated type that denotes what sort of data the name refers to, such as text, numbers, or a date.

For example

int age = 21;
Enter fullscreen mode Exit fullscreen mode

The statement declares a variable called age which is an int as it's type, which then carries a value of 21. The int part of the statement is known as a type annotation, which tells Dart what the type is.

Let's pause for a while

  • You will always use = and == now let's see their differences, The equals sign = is known as the assignment operator because it assigns a value to a variable. This is different than the equals sign you are familiar with from math. That equals sign is more like the == equality operator in Dart. We will learn more on next chapter.

To change the value of a variable simple you can assign another value of the same type.

int age=21;
age=22;
Enter fullscreen mode Exit fullscreen mode

Type safety
Dart is a type safe language, which means once you assign a variable it's data type you cannot change it later, example:

int age = 21;
age = 21.9; // This is not allowed 
Enter fullscreen mode Exit fullscreen mode

In our example above we first defined a variable with type int and give it value, in the next line we're assigning the same variable with a decimal value which is double in flutter.
If we run this program we will get an error.

But whenever you find yourself in a situation where you want the same variable to be assigned int or float you can use num. The num type can be either an int or a double, so the first two assignments work.

And if you don't care about type safety at all you can use dynamic type which will allow you to use any data type without getting any error.

Type inference
You can define a variable without telling it's type and dart will do that for you. By using var dart allows you to define a variable without telling it's data type and dart will be smart enough to know what type it is.

But by using var keyword dart will still apply type safety, example:

var age = 10;
age = 15; // OK
age = 3.14159; // No
Enter fullscreen mode Exit fullscreen mode

Constants

When we define variables we may encounter in a situation where we don't want the variable value to be changed after it's assigned. Dart has two different types of “variables” whose values never change. They are declared with the const and final keywords, and the following sections will show the difference between the two.

Having variable's that value can change can cause problems in the program, but we use them mostly, this type of variable are called mutable data. It's easy to lose track which variable value can change within our programs, we encourage to use constants whenever possible. Unchangeable variables are known as immutable data.

We use const in Dart to create a constant variable, example:

const age=22;
Enter fullscreen mode Exit fullscreen mode

Now we're telling dart that age value will never change. If we try to do the following we will get an error.

age=21; // this will result an error
Enter fullscreen mode Exit fullscreen mode

final constants

When we use const we always know the value of our variable before running our program, but there's a situation where you want to define a variable as a constant but it's value is unknown until you run your program.

In Dart const is only used for compile-time constants; that is, for values that can be determined by the compiler before the program ever starts running.

If you can’t create a const variable because you don’t know its value at compile time, then you must use the final keyword to make it a runtime constant.

For example, fetching data from the server, asking user data from an input and more.

Conclusion
And that's all for our article, before you continue more try to learn more about our topic today from different resource and do more practice.

💖 💪 🙅 🚩
fluttertanzania
Flutter Tanzania

Posted on October 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related