- In this post we are going to talk about Dart programming.
- Dart is an open-source general-purpose programming language.
- Dart is an object-oriented language with C-style syntax which can optionally trans compile into JavaScript. It supports a varied range of programming aids like interfaces, classes, collections, generics, and optional typing.
- Dart is the programming language which is used to access all the methods in Flutter SDK.
- In this post I am going to explain the basics of Dart. You don't need to master in Dart the basics is enough to get started.
- You can practice the dart here.
- Dart is similar to Java script and Java. The program starts with
main()
function.
void main(){
print("Hello Dart")
}
<!-- Both are same -→
void main() => print("Hello Dart")
- (=>) Arrow Funtion lets us make things easier.When your returning a single method or statement you don't need to wrap it with in curly braces and you can avoid using
return
statement as above. - Arrow functions are a concise mechanism to represent functions. These functions are also called as Lambda functions.
Dart consists of
- Variables and Operators
- Classes
- Functions
- Expressions and Programming Constructs
- Decision Making and Looping Constructs
- Comments
- Libraries and Packages
Comments
// this is single line comment
/* This is a
Multi-line comment
*/
DataTypes
Numbers => Integer(int),Double(double).
Strings => String.
Booleans => Boolean(bool).
Lists => List.
Maps => Map.
Dynamic => Dynamic(dynamic).
-
is also a data type we can assign any data type to `var` by default it assigns `String`.
```dart
void main() {
var name = "Flutter Dart";
print(name);
}
Output:
Flutter Dart
- The
const
keyword is used to represent a compile-time constant. Variables declared using theconst
keyword are implicitlyfinal
.
Operators
void main(){
int a = 10;
int b = 10;
print("Addition of $a and $b: ${a+b}");
print("Subtraction of $a and $b: ${a-b}");
print("Multiplication of $a and $b: ${a*b}");
print("Division of $a and $b: ${a/b}");
}
Output:
Addition of 10 and 10: 20
Subtraction of 10 and 10: 0
Multiplication of 10 and 10: 100
Division of 10 and 10: 1
- Here We can directly print the value of the variable by using
$
for one value and for expression we use${}
known as string Interpolation
Decision Making
- if
- if else
- else if
- switch case
void main() {
var a = 52;
if(a > 0) {
print("$a is positive");
}
else if(a < 0) {
print("$a is negative");
} else {
print("$a is neither positive nor negative");
}
// Switch case
var name = "Ramu";
switch(name) {
case "Kumar": { print("Welcome $name"); }
break;
case "Indhu": { print("Welcome $name"); }
break;
case "Ramu": { print("Welcome $name"); }
break;
default: { print("Welcome Guest"); }
break;
}
}
Output:
52 is positive
Welcome Ramu.
Loops
// For Loop
for (var i = 0;i<4;i++){
print("Value of i is $i");
}
// While Loop
var i = 0;
while(i<4){
print(i);
i++;
}
// Do while loop
var i1 = 0;
do{
print(i1);
i1++;
}while(i1<4);
Output:
// For Loop
Value of i is 0
Value of i is 1
Value of i is 2
Value of i is 3
// While Loop
Value of i is 0
Value of i is 1
Value of i is 2
Value of i is 3
// Do while loop
Value of i is 0
Value of i is 1
Value of i is 2
Value of i is 3
Lists and Map
Lists in some of the programming languages it is also called Arrays both are same plays a crucial role in order to store a large amount of data in the form of Lists.
The Map object is a simple key/value pair. Keys and values in a map may be of any type. A Map is a dynamic collection. In other words, Maps can grow and shrink at run time.
// List
var list = [1,2,3,4,5,6,7,8,9];
print(list);
//Assigning values dynamically to list
var list1 = List(2);
list1[0] = 'Ramu';
list1[1] = 'Kumar';
print(list1);
//Map
var dict = {'Username':'Ramu','Password':'1111'};
print(dict);
var dict1 = {};
dict1['1'] = 'Ramu';
dict1['20'] = 'Kumar';
print(dict1);
Output:
//List
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[Ramu, Kumar]
//Map
{Username: tom, Password: pass@123}
{1: Ramu, 20: Kumar}
Functions
- Functions are the building blocks of readable, maintainable, and reusable code. A function is a set of statements to perform a specific task step by step. Functions organize the program into logical blocks of code.
void showMyName(String name){
print("Welcome $name");
}
showMyName("Ramu");
Output:
Welcome Ramu.
Factorial of a given number:
void main() => print(factorial(3));
factorial(num){
if(num<=0){
return 1;
}
else{
return num = factorial(num-1) * num;
}
}
Output:
6
OOPS (Object Oriented Programming)
- Dart is an object-oriented language. It supports object-oriented programming features like classes, interfaces, etc.
- A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object. Dart gives built-in support for this concept called class.
void main() {
Name n1 = Name("Bugudi","Ramu");
}
class Name {
Name(String firstName,String lastName) {
print("Welcome $firstName $lastName");
}
}
Output:
Welcome Bugudi Ramu
- Here we have declared a class with the help of
class
keyword and given Name as class name. - A constructor is a special function of the class that is responsible for initializing the variables of the class.
- Then we created a constructor
Name(String firstName,String lastName){}
which takes two parameters firstName and lastName.Then we printed the message to console. - In the
main()
method we instantiatedn1
object forName
class and passed the required two parameters.
I think this knowledge is more than enough to get started with Flutter.From the next tutorial onwards we are going to dive deep to learn core Flutter Framework.See you guys!
Top comments (0)