Dart Exercise
01-Fundamentals
playground.dart
Code
main() {
var firstName = 'Mahmud';
String lastName = 'Ahsan';
print(firstName + ' ' + lastName);
}
Output
Mahmud Ahsan
playground2.dart
Code
main() {
stdout.writeln('What is your name: ?');
String name = stdin.readLineSync();
print('My nae is $name');
}
Output
What is your name: ?
aa
My nae is aa
02-Data Types
playground3.dart
Code
main() {
int amount1 = 100;
var amount2 = 200;
print('Amount1: $amount1 | Amount2: $amount2 \n');
double dAmount1 = 100.11;
var dAmount2 = 200.22;
print('dAmount1: $dAmount1 | dAmount2: $dAmount2 \n');
String name1 = 'Mahmud';
var name2 = 'Ahsan';
print('My name is: $name1 $name2 \n');
bool isItTrue1 = true;
var isItTrue2 = false;
print('isItTrue1: $isItTrue1 | isItTrue2: $isItTrue2 \n');
dynamic weakVariable = 100;
print('WeakVariable 1: $weakVariable \n');
weakVariable = 'Dart Programming';
print('WeakVariable 2: $weakVariable');
}
Output
Amount1: 100 | Amount2: 200
dAmount1: 100.11 | dAmount2: 200.22
My name is: Mahmud Ahsan
isItTrue1: true | isItTrue2: false
WeakVariable 1: 100
WeakVariable 2: Dart Programming
03-String, Type Conversion, Constant, Null
playground4.dart
Code
main() {
var s1 = 'Single quotes work well for string literals.';
var s2 = 'Double quotes work just as well.';
var s3 = 'It\'s easy to escape the string delimiter.';
var s4 = "It's even easier to use the other delimiter.";
print(s1);
print(s2);
print(s3);
print(s4);
print('');
//RAW String
var s = r'In a raw string, not even \n gets special treatment.';
print(s);
}
Output
Single quotes work well for string literals.
Double quotes work just as well.
It's easy to escape the string delimiter.
It's even easier to use the other delimiter.
In a raw string, not even \n gets special treatment.
playground5.dart
Code
main() {
var age = 35;
var str = 'My age is : $age';
print(str);
}
Output
My age is : 35
playground6.dart
Code
main() {
var s1 = '''You can create multi-line strings like this one.
''';
var s2 = """This is also a multi-line string.""";
print(s1);
print(s2);
}
Output
You can create multi-line strings like this one.
This is also a multi-line string.
playground7.dart
Code
main() {
// String -> int
var one = int.parse('1');
assert(one == 1);
// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);
// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');
// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');
}
playground8.dart
Code
main() {
const aConstNum = 0; // int constant
const aConstBool = true; // bool constant
const aConstString = 'a constant string'; // string constant
print(aConstNum);
print(aConstBool);
print(aConstString);
print(aConstNum.runtimeType);
print(aConstBool.runtimeType);
print(aConstString.runtimeType);
}
Output
0
true
a constant string
int
bool
String