Dart Programming
Dart Programming
Dart is an open-source, general-purpose, object-oriented programming
language with C-style syntax developed by Google in 2011.
The purpose of Dart programming is to create a frontend user interfaces
for the web and mobile apps.
It is under active development, compiled to native machine code for
building mobile apps, inspired by other programming languages such as
Java, JavaScript, C#, and is Strongly Typed.
Since Dart is a compiled language so you cannot execute your code
directly; instead, the compiler parses it and transfer it into machine
code.
It supports most of the common concepts of programming languages
like classes, interfaces, functions, unlike other programming languages.
Dart language does not support arrays directly. It supports collection,
which is used to replicate the data structure such as arrays, generics,
and optional typing.
The following example shows simple Dart programming.
1. void main() {
2. for (int i = 0; i < 5; i++) {
3. print('hello ${i + 1}');
4. }
5. }
Data Type
Dart is a Strongly Typed programming language.
It means, each value you use in your programming language has a type
either string or number and must be known when the code is compiled.
The most common basic data types used in the Dart programming
language.
Prepared by: G Sudhakar Raju Page 1 of 4
Dart Programming
Simple Examples
Online Compiler: https://dartpad.dev/
Example 1:
void main() {
print("Hello World!");
}
Output:
Hello World!
Example 2:
void main(){
var firstName = "Sudhakar";
var lastName = "Raju";
print("Full name is $firstName $lastName");
}
Output:
Full name is Sudhakar Raju
Example 3:
void main() {
int num1 = 10; //declaring number1
int num2 = 3; //declaring number2
// Calculation
int sum = num1 + num2;
int diff = num1 - num2;
int mul = num1 * num2;
double div = num1 / num2;
// It is double because it outputs number with decimal.
Prepared by: G Sudhakar Raju Page 2 of 4
Dart Programming
// displaying the output
print("The sum is $sum");
print("The diff is $diff");
print("The mul is $mul");
print("The div is $div");
}
Output:
The sum is 13
The diff is 7
The mul is 30
The div is 3.3333333333333335
Example 4:
// Main function
void main() {
// Creating a list
var myList = [121, 12, 33, 14, 3];
// Declaring and assigning the
// largesValue and smallestValue
var largesValue = myList[0];
var smallestValue = myList[0];
for (var i = 0; i < myList.length; i++) {
Prepared by: G Sudhakar Raju Page 3 of 4
Dart Programming
// Checking for largest value in the list
if (myList[i] > largesValue) {
largesValue = myList[i];
}
// Checking for smallest value in the list
if (myList[i] < smallestValue) {
smallestValue = myList[i];
}
}
// Printing the values
print("Smallest value in the list : $smallestValue");
print("Largest value in the list : $largesValue");
}
Output:
Smallest value in the list : 3
Largest value in the list : 121
Prepared by: G Sudhakar Raju Page 4 of 4