A Comprehensive Guide For Dart Programming: Crash Course (Part — 1)
Everything you need to know to get started with Dart programming language.
Most of the time, we spend watching hours of content on youtube or other sites, and all we want is to get started with that language. If you’re going to be a better developer, then you need to learn all the basic concepts, but if you want to build things as fast as possible, then all you need is a crash course.
Why learn Dart programming?
Dart is a relatively simple, modern, and highly efficient programming language. Dart is a client-optimized programming language for apps on multiple platforms. Dart is developed by Google and used to build mobile, desktop, server, and web applications. Dart is an object-oriented, class-based, garbage-collected language with C-style syntax. Dart can compile to either native code or JavaScript.
Topics That We Are Going To Cover in All Parts:
- Hello World (All we need to learn programming language 😅)
- Comments
- Taking Input
- Data Types
- Operators
- Conditional Statements
- Loops
- Functions
- Exception Handling
- Collections
- Object-Oriented Programming
Topics That We Are Going To Cover in This Part:
- Hello World
- Comments
- Taking Input
- Data Types
- Operators
1. Hello World:
Main is a special, required, a top-level function where app execution starts. Every app must have a top-level main()
function, which serves as the entry point to the app.
So, without wasting time, let’s write our hello world program.
void main() {
print(“Hello World”);
}
2. Comments
Comment in a program is like a note for all the developer who is going to read that program.
Comments are only for humans at compile time compiler going to ignore it.
void main() {
//it's single line comment
/* Multiple
Line
Comment
*/
print(“Hello World”);
}
3. Taking Input
You can make standard input from the user through the console via the use of the readLineSync() function, and for that, you have to import a library dart:io from the Dart.
import ‘dart:io’;void main(List<String> args) {
stdout.write(“What’s your name: “);
String name = stdin.readLineSync();
print(name);
}
4. Data Types
Before diving into data types first, let’s talk about variables.
Variables
Variable is a reference or symbolic name to the memory location of the computer. In simple words, it’s like your primary school’s math equation, e.g., x = 5, where we stored 5 in x. We can change its value according to our needs. For example, we can replace it’s value to x = ‘name’ or whatever we want.
Some rules for declaring variables:
- Variable can be a combination of letters in lowercase (a to z), uppercase (A to Z), digits (0 to 9), or an underscore (_).
- A variable cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
- Keywords cannot be used as a Variable name.
Numbers
There are two types of numbers in Dart:
- int:
In Dart, values of int can be from -263 to 263–1.
2. double:
64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard.
void main() {
// Numbers: int and double
int age = 22;
double num = 10.5;
print(age + num);
}
String
A Dart string is a sequence of UTF-16 code units. You can use either single or double quotes to create a string:
You can put the value of expression inside a string by using ${expression}
. If the expression is an identifier, you can skip the {}.
void main() {
// String and String iterpolation
String name = “Rajesh Berwal”;
print(‘My nname is $name and the length of the name with space is ${name.length}’);
}
Boolean
To represent boolean values, Dart has a type named bool
. Only two objects have type bool: the boolean literals true
and false
void main() {
// Boolean
bool isWorking = true;
print(“Are you working: ${isWorking}”);
bool notWorking = false;
print(“Are you working: ${isWorking}”);
}
Constants and Final
If you never intend to change a variable, use final
or const.
void main() {
// Creating Constants
/*
-> final variable is only initialized when it’s accessed
-> const variable is initialized at the compile time
-> main difference const keyword get memory location at the compile time
-> but final variable only when it is accessed
*/
const daysInLeapYear = 366;
print(‘Days in leap year: $daysInLeapYear’);final pi = 3.14;
print(pi);
}
Declaring Variable Dynamically
Variables declared without a static type are implicitly declared as dynamic.
void main() {
// We can also declare variable dynamically
var something = “Somewhere”;
print(something);
var num = 5;
print(num);
}
5. Operators:
Arithmetic operators
Dart supports the usual arithmetic operators, as shown in the following example:
void main() {
print(2 + 3 == 5);
print(2 - 3 == -1);
print(2 * 3 == 6);
print(5 / 2 == 2.5); // Result is a double
print(5 ~/ 2 == 2); // Result is an int
print(5 % 2 == 1); // Remainder print('5/2 = ${5 ~/ 2} r ${5 % 2}' == '5/2 = 2 r 1');
}
Comparison (Relational) operators
Here’s an example of using each of the equality and relational operators:
void main() {
print(2 == 2);
print(2 != 3);
print(3 > 2);
print(2 < 3);
print(3 >= 3);
print(2 <= 3);
}
Logical (Boolean) operators
You can invert or combine boolean expressions using logical operators.
!expr inverts the following expression (changes false to true, and vice versa)|| logical OR&& logical AND
Here’s an example of using logical operators:
if (!done && (col == 0 || col == 3)) {
// ...Do something...
}
Bitwise operators
You can manipulate the individual bits of numbers in Dart. Usually, you’d use these bitwise and shift operators with integers.
& AND
| OR
^ XOR
~ exprUnary bitwise complement (0s become 1s; 1s become 0s)
<< Shift left
>> Shift right
Here’s an example of using bitwise and shift operators:
void main() {
final value = 0x22;
final bitmask = 0x0f;print((value & bitmask) == 0x02); // AND
print((value & ~bitmask) == 0x20); // AND NOT
print((value | bitmask) == 0x2f); // OR
print((value ^ bitmask) == 0x2d); // XOR
print((value << 4) == 0x220); // Shift left
print((value >> 4) == 0x02); // Shift right
}
Assignment operators
As you’ve already seen, you can assign values using the =
operator. To assign only if the assigned-to variable is null, use the ??=
operator.
// Assign value to a
a = value;
// Assign value to b if b is null; otherwise, b stays the same
b ??= value;
Compound assignment operators such as +=
combine operation with an assignment.
= –= /= %= >>= ^= += *= ~/= <<= &= |=
The following example uses assignment and compound assignment operators:
void main() {
var a = 2; // Assign using =
a *= 3; // Assign and multiply: a = a * 3
print(a == 6);
}
Take my advice if you want to be a better developer then work on the initial term of programming because syntax changes in different programming languages, but concepts remain the same. A crash course is designed to help you get started in the dart programming language so you can create your application faster. For better understanding, learn the inner work of that language.
For a better understanding of the time complexity of a program, you can check out our article: