0% found this document useful (0 votes)
4 views9 pages

Dart Programming Notes - Part 1 - Fundamentals & Syntax

Dart is a client-optimized programming language developed by Google for multi-platform app development, emphasizing fast performance and developer productivity. The document outlines the installation, basic program structure, variable declaration, data types, control flow, functions, and null safety in Dart. It serves as an introduction to the fundamental concepts of Dart programming, with further topics to be covered in subsequent sections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Dart Programming Notes - Part 1 - Fundamentals & Syntax

Dart is a client-optimized programming language developed by Google for multi-platform app development, emphasizing fast performance and developer productivity. The document outlines the installation, basic program structure, variable declaration, data types, control flow, functions, and null safety in Dart. It serves as an introduction to the fundamental concepts of Dart programming, with further topics to be covered in subsequent sections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Dart Programming Notes - Part 1: Fundamentals & Syntax

What is Dart?
Dart is a client-optimized programming language developed by Google in 2011. It's designed for
building fast apps on any platform, with a focus on:

Multi-platform development (mobile, web, desktop, server)


Fast performance with ahead-of-time (AOT) and just-in-time (JIT) compilation

Developer productivity with hot reload and strong tooling


Type safety with sound null safety

Getting Started

Installation & Setup

bash

# Install Dart SDK


# Windows: choco install dart-sdk
# macOS: brew install dart
# Linux: apt-get install dart

# Verify installation
dart --version

Basic Program Structure

dart

// Entry point of every Dart program


void main() {
print('Hello, World!');
}

// With command line arguments


void main(List<String> args) {
print('Arguments: $args');
}

Variables & Data Types

Variable Declaration
dart

// Explicit type declaration


String name = 'John Doe';
int age = 25;
double salary = 50000.50;
bool isEmployed = true;

// Type inference with 'var'


var city = 'New York'; // String inferred
var population = 8000000; // int inferred
var temperature = 72.5; // double inferred

// Dynamic type (avoid unless necessary)


dynamic flexibleVar = 'Can be anything';
flexibleVar = 42;
flexibleVar = true;

Constants

dart

// final - runtime constant (set once)


final String currentUser = getUserName();
final DateTime now = DateTime.now();

// const - compile-time constant


const double pi = 3.14159;
const List<String> colors = ['red', 'green', 'blue'];

// late - lazy initialization


late String heavyComputation;
void initializeData() {
heavyComputation = performHeavyTask();
}

Core Data Types

Numbers
dart

// Integers
int age = 25;
int hexValue = 0xFF; // 255 in decimal
int binaryValue = 0b1010; // 10 in decimal

// Doubles
double price = 19.99;
double scientific = 1.5e3; // 1500.0

// Number operations
int a = 10, b = 3;
print(a + b); // 13
print(a - b); // 7
print(a * b); // 30
print(a / b); // 3.3333...
print(a ~/ b); // 3 (integer division)
print(a % b); // 1 (remainder)

Strings

dart

// String literals
String singleQuote = 'Hello World';
String doubleQuote = "Hello World";
String multiline = '''
This is a
multiline string
''';

// String interpolation
String name = 'Alice';
int age = 30;
String greeting = 'Hello, $name! You are $age years old.';
String expression = 'Next year you will be ${age + 1}.';

// String methods
String text = 'Dart Programming';
print(text.length); // 16
print(text.toUpperCase()); // DART PROGRAMMING
print(text.toLowerCase()); // dart programming
print(text.substring(0, 4)); // Dart
print(text.split(' ')); // ['Dart', 'Programming']
print(text.contains('Dart')); // true
Booleans

dart

bool isActive = true;


bool isComplete = false;

// Boolean operations
bool a = true, b = false;
print(a && b); // false (AND)
print(a || b); // true (OR)
print(!a); // false (NOT)

Control Flow

Conditional Statements
dart

// if-else
int score = 85;
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else if (score >= 70) {
print('Grade: C');
} else {
print('Grade: F');
}

// Ternary operator
String result = score >= 60 ? 'Pass' : 'Fail';

// Switch statement
String day = 'Monday';
switch (day) {
case 'Monday':
case 'Tuesday':
case 'Wednesday':
case 'Thursday':
case 'Friday':
print('Weekday');
break;
case 'Saturday':
case 'Sunday':
print('Weekend');
break;
default:
print('Invalid day');
}

Loops
dart

// for loop
for (int i = 0; i < 5; i++) {
print('Count: $i');
}

// for-in loop
List<String> fruits = ['apple', 'banana', 'orange'];
for (String fruit in fruits) {
print('Fruit: $fruit');
}

// while loop
int count = 0;
while (count < 3) {
print('While count: $count');
count++;
}

// do-while loop
int num = 0;
do {
print('Do-while: $num');
num++;
} while (num < 3);

Functions

Basic Functions
dart

// Function with return type


String greet(String name) {
return 'Hello, $name!';
}

// Arrow function (single expression)


String greetShort(String name) => 'Hello, $name!';

// Function with multiple parameters


int add(int a, int b) {
return a + b;
}

// Function with optional parameters


String introduce(String name, [int? age]) {
if (age != null) {
return 'I am $name, $age years old.';
}
return 'I am $name.';
}

// Named parameters
String createUser({required String name, int age = 0, String? email}) {
return 'User: $name, Age: $age, Email: ${email ?? 'Not provided'}';
}

// Usage
print(greet('Alice'));
print(add(5, 3));
print(introduce('Bob', 25));
print(createUser(name: 'Charlie', email: 'charlie@email.com'));

Function Types & Higher-Order Functions


dart

// Function as parameter
void executeFunction(Function callback) {
callback();
}

// Function returning function


Function makeMultiplier(int factor) {
return (int value) => value * factor;
}

// Usage
executeFunction(() => print('Callback executed!'));
var double = makeMultiplier(2);
print(double(5)); // 10

Null Safety
Dart has sound null safety, meaning non-nullable types cannot contain null.

dart

// Non-nullable types
String name = 'John'; // Cannot be null
// String name = null; // Error!

// Nullable types
String? nullableName = null; // Can be null
int? nullableAge; // null by default

// Null-aware operators
String? firstName = null;
String? lastName = 'Doe';

// ?? (null coalescing)
String displayName = firstName ?? 'Unknown';

// ??= (null assignment)


firstName ??= 'Anonymous';

// ?. (null-aware access)
print(firstName?.length); // null if firstName is null

// ! (null assertion)
String definitelyNotNull = firstName!; // Throws if null
This covers the fundamental concepts of Dart programming. The next response will cover Collections,
Object-Oriented Programming, and more advanced features.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy