A Simple Story on Dart Programming Language. Part - 1
Ridoy Paul
Software Engineer (PHP, Laravel, Bootstrap, React JS, Next JS, Vanilla JS, ES6, MySql, Firebase, Restful API, Problem Solving, Agile )
What is Dart programming language?
Dart is a versatile, open-source programming language created by Google that's designed for building: mobile apps, web apps, ?server-side apps, and even ?desktop apps.
Key characteristics:
Key features:
Getting Started with Dart:
Hello World: Your First Dart Program,
void main() {
print("Hello World");
}
Variables:
Dart variables serve as containers for data storage, declared with var and supporting explicit or dynamic typing. Constants (`const` or final) provide compile-time evaluation, and string interpolation embeds variables in strings. Dart's strong null safety and type inference enhance flexibility, facilitating effective data manipulation in programming.
int n1 = 5; // explicitly typed
var n2 = 4; // type inferred
// n2 = "abc"; // error
dynamic n3 = 4; // dynamic means n3
// can take on any type
n3 = "abc";
double n4; // n4 is null
String s1 = 'Hello, world!';
var s2 = "Hello, world!";
Constants:
Dart constants, declared using const or final, provide values evaluated at compile-time. They ensure immutability and are fundamental for storing unchanging data throughout the program execution.
const PI = 3.14; // const is used
// for compile-time constant
final area = PI 5 5;
// final variables can only be set once
Conditional Expressions:
Dart allows concise condition-based value assignment with the ? : syntax. Ideal for compact ternary operations.
var grade = 3;
var reply = grade > 3 ? "Cool" : "Not cool";
var input; // input is null
var age = input ?? 0;
print(age); // 0
Functions:
Modular blocks of code, functions perform specific tasks. They enhance code readability and reusability.
void doSomething() {
print("doSomething()");
}
int addNums1(num1, num2, num3) {
return num1 + num2 + num3;
}
doSomething();
print(addNums1(1, 2, 3));
Arrow Syntax:
Shortened syntax for one-line functions, enhancing code brevity and clarity.
领英推荐
void doSomethingElse() {
doSomething();
}
// the above can be rewritten using
// arrow syntax
void doSomethingElse() => doSomething();
Optional Positional Parameters:
Functions accept variable arguments, providing flexibility. Parameters have default values if not provided.
int addNums2(num1, [num2 = 0, num3 = 0]) {
return num1 + num2 + num3;
}
print(addNums2(1));
print(addNums2(1, 2));
print(addNums2(1, 2, 3));
Named Parameters:
Functions accept parameters by name, enhancing clarity and allowing optional values.
int addNums3({num1, num2, num3}) {
return num1 + num2 + num3;
}
print(addNums3(num1: 1, num2: 2, num3: 3));
Optional Named Parameters:
Like named parameters, but optional. Provide flexibility with clear, optional values in function calls.
int addNums4(num1, {num2 = 0, num3 = 0}) {
return num1 + num2 + num3;
}
print(addNums4(1));
print(addNums4(1, num3: 2));
print(addNums4(1, num2: 5, num3: 2));
Parsing:
Dart converts strings to numeric values using parse and tryParse methods, handling conversion errors.
var s1 = "123";
var s2 = "12.56";
var s3 = "12.a56";
var s4 = "12.0";
print(num.parse(s1)); // 123
print(num.parse(s2)); // 12.56
// FormatException: 12.a56
print(num.tryParse(s3)); // null
String Interpolation:
Dart's string interpolation allows seamless embedding of variables or expressions within strings using ${}. Enhances readability and simplifies the construction of dynamic string messages and concatenation.
var s1 = "Hello";
var s2 = "world";
var s3 = s1 + ", " + s2;
var s = "${s3}!";
print(s); // Hello, world!
print("Sum of 5 and 6 is ${5 + 6}");
List (Arrays):
Dynamic data structures in Dart that hold ordered elements. Modify, access, and iterate through lists.
// dynamic list
var arr = [1, 2, 3, 4, 5];
print(arr.length); // 5
print(arr[1]); // 2
arr[4] *= 2;
print(arr[4]); // 10
arr.add(6);
print(arr); // [1, 2, 3, 4, 10, 6]
// List assignment and modification
List arr2;
arr2 = arr;
arr2[1] = 9;
print(arr); // [1, 9, 3, 4, 10, 6]
print(arr2); // [1, 9, 3, 4, 10, 6]
// fixed size list
var arr3 = List(3);
print(arr3); // [null, null, null]
// arr3.add(5); // Uncaught exception:
// Unsupported operation: add
Map:
Key-value pairs in Dart, where each value is associated with a unique key. Useful for efficient data retrieval and organization.
var details = {"name": "Sam", "age": "40"};
print(details);
var devices = Map();
var apple = ["iPhone", "iPad"];
var samsung = ["S10", "Note 10"];
devices["Apple"] = apple;
devices["Samsung"] = samsung;
for (String company in devices.keys) {
print(company);
for (String device in devices[company]) {
print(device);
}
}