Skip to content

Instantly share code, notes, and snippets.

@houcem-h
Last active March 7, 2022 10:33
Show Gist options
  • Save houcem-h/f9fb04d85f7151b21bbb551088a3486c to your computer and use it in GitHub Desktop.
Save houcem-h/f9fb04d85f7151b21bbb551088a3486c to your computer and use it in GitHub Desktop.
void main() {
// ---------- 1- numbers
int counter; // null
double priceEach; // null
int counter1 = 0;
double texRate = 20;
num counter2 = 10; // int
num price = 14.16; // double
int? age = int.tryParse("25"); // parse a string to int
// ---------- 2- strings
String student1 = "Emna";
//parsing to string
int age2 = 19;
String stringAge = age2.toString();
//string operations
String country = "Tunisia";
print(country.toUpperCase());
print(country.toLowerCase());
print(country.substring(0, 3));
// ---------- 3- dynamic types
var phone = "(+216)22334455";
print(phone.runtimeType);
// phone = 22334455; // error: value type int can't be assigned to variable type string
dynamic id = "user12345";
print(id.runtimeType); // String
id = 12345;
print(id.runtimeType); // int
// ---------- 4- constants
final double taxRate2 = 20;
//taxRate2 = 19; // error: final variable can be only set once
final List<int> tab = [12, 73, 55];
tab.add(38);
print(tab);
const List<int> tab2 = [12, 73, 55];
//tab2.add(38); // error : unsupported operation
// ---------- 5- collections
// 1. list
List<int> numbers = [];
numbers.add(34);
numbers.add(76);
numbers.add(50);
numbers.add(312);
numbers.add(47);
print(numbers);
// clear() : remove array content
// numbers.clear();
// print(numbers);
// indexOf(element) : first index of element in the list.
print(numbers.indexOf(50)); // 2
print(numbers.indexOf(55)); // -1
// remove(value) : removes the first occurence of value
numbers.remove(50);
print(numbers);
//removeAt(index) : removes value at position index
//numbers.removeAt(1);
//print(numbers);
// removeLast() remove the value at the last position
//int elem = numbers.removeLast();
//print(elem);
//print(numbers);
// length
print(numbers.length);
numbers.sort();
print(numbers);
// 2. Maps
// declaration 1
var student = Map();
student['name'] = "Jasser";
student['age'] = 20;
print(student);
// declaration 2
var student2 = {'name': "Emna", 'address': "Tunis"};
print(student2);
// values
print(student['name']);
print(student2.values);
}
void main() {
// simple function
sayHello();
// function with return
String message = getMessage();
print(message);
// or
print(getMessage());
// function with params
print(getMessage2("Mahmoud"));
// function with optional params
print(getMessage3("Emna"));
print(getMessage3("Emna", "Mrs"));
// anonymous function
const fruits = ['apples', 'bananas', 'oranges'];
fruits.forEach((fruit) {
print('${fruits.indexOf(fruit)}: $fruit');
});
}
// simple function
void sayHello() {
print('Hello Dart!');
}
// function with return
String getMessage() {
String message = "Hello Dart!";
return message;
}
// function with params
String getMessage2(String username) {
return "Hello ${username.toUpperCase()}";
}
// function with optional params
String getMessage3(String username, [String? title]) {
String message;
if(title == null) {
message ="Hello ${username.toUpperCase()}";
} else {
message = "Hello $title ${username.toUpperCase()}";
}
return message;
}
// arrow functions / lambda functions
String getMessage4(String username) {
return "Hello ${username.toUpperCase()}";
}
String getMessage5(String username) => "Hello ${username.toUpperCase()}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment