A Simple Story on Dart Programming Language. Part - 1

A Simple Story on Dart Programming Language. Part - 1

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:

  1. Object-oriented: Everything in Dart is an object, promoting modularity and code reusability.
  2. C-style syntax: It's familiar to those with experience in languages like C++, Java, and JavaScript, making it easy to learn.
  3. Strong typing: Variables have defined types, ensuring code safety and preventing errors.
  4. Focused on client-side development: It excels at creating fast, responsive user interfaces.
  5. Cross-platform: Compiles to native machine code for mobile apps, JavaScript for web apps, and even WebAssembly.
  6. Flutter's foundation: Dart is the core language behind Flutter, Google's popular cross-platform app development framework.

Key features:

  1. Sound null safety: Eliminates null reference errors, leading to more robust code.
  2. Async/await: Simplifies handling asynchronous operations, essential for modern apps.
  3. Isolates: Independent workers for concurrency and security, enabling parallel execution and error isolation.
  4. Hot reload: See code changes reflected in running apps almost instantly, boosting development speed.
  5. Rich ecosystem: Wide range of libraries, tools, and resources available.

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);
  }
}        


要查看或添加评论,请登录

社区洞察

其他会员也浏览了