Dart Programming Language
Dart Programming Language
Dart
https://dartpad.dartlang.org/
Sample syntax
void main() {
print('hello world');
}
Dart syntax
Whitespace and Line Breaks- Dart ignores spaces, tabs, and newlines that appear in
programs
Dart is case-sensitive- means that Dart differentiates between uppercase and lowercase
characters.
Statements end with a Semicolon -Each dart statement must end with a semicolon (;).
Comments in Dart
Single-line comments ( // ) − Any text between a "//" and the end of a line is treated as a
comment
Multi-line comments (/* */) − These comments may span multiple lines.
Data type in dart
Variable names are called identifiers. Following are the naming rules for an identifier −
• Identifiers cannot be keywords.
• Identifiers can contain alphabets and numbers.
• Identifiers cannot contain spaces and special characters, except the underscore (_) and the
dollar ($) sign.
• Variable names cannot begin with a number.
• Variable name are case sensitive.
• Int num;
• NUM=10;
Numbers
Dart Integer
Dart Double
used to store a 64 bit floating-point number
double pi=3.14;
Dart String
used to store series or sequence of character,letters,numbers and specical character.
String name='smith';
variable
Dart Boolean
used to stored true or false
bool isValid=true;
Dart lists
is used to represent a collection of objects.
var list=[1,2,3];
Dart Maps
is used to represent a set of values as key-value pairs.
var weekDays={'Day1':'Mon','Day1':'Tue','Day1':'Wed'};
Type Syntax
A variable must be declared before it is used. Dart uses the var keyword to achieve the
same. The syntax for declaring a variable is as given below .
syntax
void main() {
var a;
print(a);
}
Operator in dart
Add +
Subtract -
Multiply *
Divide /
Get the remainder of an integer division %
Increment ++
decrement --
Equality and Relational Operators
if(boolean_expression){
// statement(s) will execute if the Boolean expression is true.
} else {
// statement(s) will execute if the Boolean expression is false.
}
If the Boolean expression evaluates to be true, then the if block
of code will be executed, otherwise else block of code will be
executed.
Example - syntax
Live Demo
void main() {
var num = 12;
if (num % 2==0) {
print("Even");
} else {
print("Odd");
}
}
Parse()-
The parse() static function allows parsing a string containing numeric literal into a
number.
void main() {
print(num.parse('12'));
print(num.parse('10.91'));
}
${}
use "${}" can be used to interpolate the value of a Dart expression within strings.
void main() {
int num1=10;
int num2=20;
int sum=num1+num2;
print ("total sum result is ${sum}");
}
Operator +
The operator plus (+) is a commonly used mechanism to concatenate / interpolate strings.
void main() {
String str1 = "hello";
String str2 = "world";
String res = str1+str2;
3 trim() Returns the string without any leading and trailing whitespace.
replaceAll() Replaces all substrings that match the specified pattern with a
5
given value.
Methods to Manipulate Strings
substring() Returns the substring of this string that extends from startIndex,
7
inclusive, to endIndex, exclusive.
void main() {
for(int i=1;i<=5;i++){
print ("Number is${i}");
}
}
While loop
void main() {
int i=1;
while(i<5){
print("Number is ${i}");
i++;
}
}
Do while loop
void main() {
int i=1;
do {
print("Number is ${i}");
i++;
}while(i<=5);