Contents
Part 1: Introduction To Dart
- What is Dart
- Dart’s Hello World!
- Comments
- Operators
- Control Flow
Part 2: Dart Data Types
- Basic Data Types
- Complex Data Types
- Dynamic Data Type
- Final & Const
Part 3: Dart Functions
- Basic Functions
- Types of functions
- Scope
Part 4: Dart Object-Oriented Programming
- Classes
- Constructors
- Inheritance
- Enums
Part 1: Introduction To Dart
1. What Is Dart
Dart is a client-optimized programming language for fast apps on multiple platforms. It is developed by Google and is used to build mobile, desktop, backend and web applications. Dart is an object-oriented, class defined, garbage-collected language using a C-style syntax that transcompiles optionally into JavaScript.
Why Does Flutter Use Dart?
Dart supports Just In Time (JIT) as well Ahead Of Time (AOT) compiling.
Just In Time (JIT)
Just In Time (JIT) allows fast development called Hot-Reloading that use in the Flutter App Development.
Ahead Of Time (AOT)
Ahead Of Time (AOT) compiler converts Dart into an efficient native code which makes flutter faster.
2. Dart’s Hello World
We are going to write a very simple program that prints “Hello World!” using Repl Editor as below. Dart uses .dart extension to write Dart code. You can see there is a file `main.dart`. If you hit the play button top center of the embedded editor. It will compile the dart file and print “Hello World!”. You can practice writing something else with the following editor. Void main is an entry point of the Dart file. “print” is used to print output “Hello World!”.
3. Comments
Dart support 3 types of comments
Block comment
/*
The code below will
print Hello World!
*/void main() {
print("Hello World!");
}
Inline comment
void main() {
// String name;
print("Hello World!");
}
Documentation comment
class Box {
/// The value this wraps.
var _value; /// True if this box contains a value.
bool get hasValue => _value != null;
}
5. Control Flow
Control flow in the Dart is similar to other high-level programming languages.
if-else:
In the following program, we have bool variable “printString” set it to true and checking that variable in the if condition that if printString is true then it will print “printString is true” else it will print “printString is false”
Switch:
In the following program, we have “fruitJuice” variable that assigned String “C” and that variable we are passing to the switch which will print “Carrot” if we have a break statement in the switch.
Switch with continue
In the following program, we have variable “fruitJuice” that assigned String “ABC” we are using “Continue” statement instead of break. Which will print
“Apple”
“Beatroot”
“Carrot”
Instead of single String
Ternary operator:
The “Ternary” operator is a kind of if/else substitute. In the following program, we are assigning value to variable “message” with the help of “Ternary” operator “?” (like if) and “:” ( like else). If “printString” is true it will print “Hello World” else “Welcome”. Try to run following program