Skip to content

Instantly share code, notes, and snippets.

@sourhub226
Last active September 5, 2021 07:25
Show Gist options
  • Save sourhub226/cc9538ec0a4818b454ce7b4459bce524 to your computer and use it in GitHub Desktop.
Save sourhub226/cc9538ec0a4818b454ce7b4459bce524 to your computer and use it in GitHub Desktop.
Dart programming language basics


Example programs with outputs for learning the fundamentals of the Dart programming language

//dart language is made up of c+python+javascript
void main(List<String> args) {
print("hello world"); // output: hello world
// datatypes available are int,double(use double for float),String, dynamic(allows to assign any datatype) ,var(like var in javascript)
int x = 34;
double y = 23.55;
print(x); // output: 34
print(x + y); // output: 57.55
String name = "sourabh";
print(name); // output: sourabh
print("My name is " + name); // output: My name is sourabh
print("My name is $name"); //formatted string // output: My name is sourabh
//string functions
print("Length of $name is " + name.length.toString()); // output: Length of sourabh is 7
//or
print("Length of $name is ${name.length}"); // output: Length of sourabh is 7
dynamic num = 23;
print(num); // output: 23
num = "number";
print(num); // output: number
var a = 12;
var b = "sourabh";
print(a); // output: 12
print(b); // output: sourabh
}
/* Output after execution:
hello world
34
57.55
sourabh
My name is sourabh
My name is sourabh
Length of sourabh is 7
Length of sourabh is 7
23
number
12
sourabh
*/
void main(List<String> args) {
//array or list
var data = [23, 45, 657, 798, 2123, 108];
print(data[0]); // output: 23
print(data); // output: [23, 45, 657, 798, 2123, 108]
//Loops
//for loop
for (int i = 0; i < 10; i++) {
print(i);
}
print("\n");
/*output: 0
1
2
3
4
5
6
7
8
9 */
for (int i = 0; i < data.length; i++) {
print(data[i]);
}
print("\n");
/* output:23
45
657
798
2123
108 */
for (var x in data) {
print("Num is $x");
}
print("\n");
/* output:Num is 23
Num is 45
Num is 657
Num is 798
Num is 2123
Num is 108 */
//for each loop (from javascript)
data.forEach((element) {
print(element);
});
/* output:23
45
657
798
2123
108 */
//while loop is same as in C lang
}
/* Output after execution:
23
[23, 45, 657, 798, 2123, 108]
0
1
2
3
4
5
6
7
8
9
23
45
657
798
2123
108
Num is 23
Num is 45
Num is 657
Num is 798
Num is 2123
Num is 108
23
45
657
798
2123
108
*/
dynamic getArea(length) {
return length * 4;
}
//dont use ; inside arrow fn
var getPerimeter = (length) => {
//here length parameter is mandatory
print(length), //output: 12
print(length + 4), //output: 16
print(length + 12), //output: 24
};
var getOptionalArea = ([length]) => {
//here length inside [] is optional. if argument is not mentional, it will be null
//if,else if,else is same like in C lang
if (length == null)
{
print("Please provide length"), //output: Please provide length
}
else
{
print(length), //output: 89
}
};
void main(List<String> args) {
print("main fn"); //output: main fn
var area = getArea(23);
print(area); //output: 92
getPerimeter(12);
getOptionalArea();
getOptionalArea(89);
}
/* Output after execution:
main fn
92
12
16
24
Please provide length
89
*/
class Students {
var name = "sourabh";
var age = 20;
Students() {
print("Hello!"); //output: Hello!
name = "neha";
age = 12;
}
void displayName() {
print("My name is ${this.name}"); //output: My name is neha
}
}
void main(List<String> args) {
var student = Students();
print(student); //output: Instance of 'Students'
print(student.name); //output: neha
print(student.age); //output: 12
student.displayName();
}
/* Output after execution:
Hello!
Instance of 'Students'
neha
12
My name is neha
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment