0% found this document useful (0 votes)
30 views26 pages

Dart Cheatsheet

Uploaded by

shriyabh2005
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)
30 views26 pages

Dart Cheatsheet

Uploaded by

shriyabh2005
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/ 26

1.

Basics

Variables
String name = "John"; // Explicit type
int age = 25;
double height = 5.9;
bool isStudent = true;

const pi = 3.14; // Compile-time constant


final today = DateTime.now(); // Runtime constant

dynamic name = 'John'; // Initially a String


print(name); // Prints: John
name = 25; // Now an int
print(name); // Prints: 25
name = true; // Now a bool
print(name); // Prints: true

var name = 'John'; // Dart infers 'name' is of type String


print(name); // Prints: John
name = 25; // ERROR: type 'String' is not assignable to 'int'

**When you use var, Dart will automatically infer the type of the variable based on the value
assigned to it.
Once a type is inferred, the variable’s type is fixed for that instance (it can't change). Dart knows
the variable’s type at compile-time, which means you catch type errors early.
When you use dynamic, the variable’s type is not checked at compile-time, meaning you can
assign any value to it, and its type can change at runtime.With dynamic, the compiler can't
catch errors where you try to use a value in a way that doesn't match its type. You only find out
at runtime, which can lead to bugs that are harder to debug, so avoid it as much as you can.

Null Safety

Nullable Types

String? name; // Can be null


name = "John";

Null Assertion
String? name = "John";
print(name!); // Assumes not null
Null-Aware Operators
print(name ?? "Default"); // If null, use "Default"
name?.toUpperCase(); // If not null, call method
name.toUpperCase(); //will give error if name is null
name?.toUpperCase(); //will not give error if null

● In Dart, variables declared outside functions (at the top level) are considered global
variables, and you can change their values. However, if you declare them as const or
final, their values can't be changed, so top-level vars in Dart are mutable unless
marked final or const.
2. Control Flow

If-Else
if (age > 18) {
print("Adult");
} else if (age == 18) {
print("Just an adult!");
} else {
print("Minor");
}

For Loop
for (var i = 0; i < 5; i++) {
print(i);
}

// For-in
var items = ['apple', 'banana'];
for (var item in items) {
print(item);
}

While and Do-While


dart
Copy code
while (condition) {
print("Running");
}

do {
print("Running");
} while (condition);

Switch-Case
switch (age) {
case 18:
print("Just adult!");
break;
case 25:
print("Quarter life!");
break;
default:
print("Age is just a number!");
}
case 1 -> print("Monday"); //arrow notation
Case 1 when age>=18 ->print(“yes”); //allowed

3. Functions

Defining Functions
void greet(String name) {
print("Hello $name!");
}

int add(int a, int b) {


return a + b;
}

Optional Parameters

void greet(String name, [String? title]) {


print("Hello ${title ?? ''} $name!");
}

void greet({required String name, String? title}) {


print("Hello ${title ?? ''} $name!");
}

Arrow Functions
int multiply(int a, int b) => a * b;
Named Parameters: Use {} and call with parameter names for readability.
Optional Parameters: Use [] or {} to make parameters optional.
Default Values: Provide defaults for optional parameters.

Anonymous Functions in Dart

An anonymous function (also known as a lambda or closure) is a function without a name.


They are useful when you need to pass short, reusable blocks of code, especially as callbacks.

Syntax:
(parameterList) {
// Function body
};
Example: Anonymous Function Assigned to a Variable
var add = (int a, int b) {
return a + b;
};
print(add(5, 3)); // Output: 8

int Function() add = () {


return 7;
};
print(add()); // Output: 7

4. Collections

List
var nums = [1, 2, 3];
List nums=[1,3,4];
List <int>nums=[5,8,9];

Set
var items = {'apple', 'banana'};
items.add('orange');
print(items.contains('banana'));

Map
var person = {'name': 'John', 'age': 25};
print(person['name']);
person['height'] = 5.9;
MAPS:
5. Classes and Objects
class Person {
String name;
int age;

Person(this.name, this.age);

void greet() {
print("Hi, I'm $name!");
}
}

var p = Person("John", 25);


p.greet();
Named Constructors
class Point {
double x, y;

Point(this.x, this.y);
Point.origin() : x = 0, y = 0;
}

var origin = Point.origin();

● Types of Classes in Dart:

Regular Classes: Can be instantiated.


dart
Copy code
class Animal {
void speak() {
print("Animal speaks");
}
}

Abstract Classes: Cannot be instantiated directly. They act as a blueprint for other classes.
dart
Copy code
abstract class Animal {
void speak(); // Abstract method
}

Final Classes: Cannot be subclassed.


dart
Copy code
final class Animal {
void speak() {
print("Animal speaks");
}
}

Sealed Classes: Restrict subclassing to only certain classes within the same file.
dart
Copy code
sealed class Animal {
void speak();
}


● Use of this:
○ Refers to the current instance of the class.
○ Commonly used to refer to instance variables or call instance methods.

dart
Copy code
class Person {
String name;

Person(this.name);

void greet() {
print("Hello, ${this.name}!");
}
}


● Constructor Types:
○ Default Constructor: Automatically provided if no constructors are defined.
○ Named Constructor: Custom constructor with a name.
○ Factory Constructor: Allows custom logic for instance creation.

dart
Copy code
class MyClass {
MyClass.namedConstructor();
}

Class Modifiers in Dart


● final:
○ A class marked as final cannot be subclassed.
○ Ensures that the class cannot be extended.

dart
Copy code
final class Animal {
void speak() {
print("Animal speaks");
}
}


● const:
○ Used to define compile-time constants.
○ Classes with a const constructor are used to create immutable objects.

dart
Copy code
class Point {
final int x, y;
const Point(this.x, this.y);
}


● abstract:
○ A class that cannot be instantiated but can be inherited from.
○ Can contain abstract methods (no implementation).

dart
Copy code
abstract class Animal {
void speak(); // Abstract method
}


● mixin:
○ A special class that can be reused in multiple classes without using inheritance.
○ Used for code sharing between classes.

dart
Copy code
mixin Swimmer {
void swim() {
print("Swimming...");
}
}

class Fish with Swimmer {}


● implements:
○ Forces a class to implement all methods from an interface (a class can act as an
interface).

dart
Copy code
class Animal {
void speak();
}

class Dog implements Animal {


@override
void speak() {
print("Bark");
}
}


● extends:
○ Used for inheritance, where a child class inherits from a parent class.

dart
Copy code
class Animal {
void speak() {
print("Animal speaks");
}
}

class Dog extends Animal {


@override
void speak() {
print("Dog barks");
}
}


● static:
○ Defines class-level properties or methods, not dependent on instances.

dart
Copy code
class Counter {
static int count = 0;
static void increment() {
count++;
}
}


● factory:
○ Custom constructor that doesn't necessarily create a new instance every time
(e.g., singleton pattern).

dart
Copy code
class Database {
static final Map<String, Database> _cache = {};

factory Database(String name) {


return _cache.putIfAbsent(name, () => Database._internal(name));
}

Database._internal(String name);

● }

Getters and Setters in Dart

● Getters and Setters are special methods used to access and modify an object's
properties.

Getters

● A getter allows you to access a private property of a class.


● Dart provides the get keyword to define a getter.
Syntax:

dart

Copy code

class Person {

String _name; // private field

Person(this._name); // Constructor to set name

// Getter for the private field

String get name => _name;

void main() {

var person = Person("Shriy");

print(person.name); // Accesses the name via the getter

● Key Points:
○ Use get followed by the property name.
○ The getter method returns the value of the field but doesn’t require calling it like a
function (i.e., no parentheses).

Setters

● A setter allows you to modify a private property of a class.


● Dart provides the set keyword to define a setter.

Syntax:
dart

Copy code

class Person {

String _name; // private field

Person(this._name); // Constructor to set name

// Setter for the private field

set name(String value) => _name = value;

void main() {

var person = Person("Shriy");

person.name = "Anjali"; // Modifies the name via the setter

print(person.name); // Output: Anjali

● Key Points:
○ Use set followed by the property name.
○ The setter method assigns a value to the field and allows modification of the
private property.

Combining Getters and Setters

dart

Copy code

class Person {
String _name;

Person(this._name);

// Getter

String get name => _name;

// Setter

set name(String value) {

if (value.isNotEmpty) {

_name = value;

} else {

print("Name cannot be empty");

void main() {

var person = Person("Shriy");

person.name = "Anjali"; // Setter modifies the value

print(person.name); // Getter accesses the value

}
● Key Points:
○ Getters are used for accessing the value.
○ Setters are used for modifying the value, often with validation.

6. Async Programming

1. Stream

● A Stream is a sequence of asynchronous events or data that can be listened to over


time.
● Streams provide continuous data, like user input, server responses, or sensor data.

Example:

dart
Copy code
Stream<int> counterStream() async* {
for (var i = 0; i < 5; i++) {
yield i; // Send data at each iteration
await Future.delayed(Duration(seconds: 1));
}
}

void main() async {


await for (var value in counterStream()) {
print(value); // Prints: 0, 1, 2, 3, 4 with 1-second delay
}
}

2. StreamController

● A StreamController is used to create a custom stream. It allows adding data to a


stream manually.
● You can use the sink.add() method to add data to the stream, and listeners can react
to it.

Example:

dart
Copy code
StreamController<int> controller = StreamController<int>();

void main() {
controller.stream.listen((data) {
print(data); // Prints: 10
});

controller.sink.add(10); // Manually adding data to stream


controller.close(); // Close the stream after use
}

3. StreamSubscription

● When you listen to a stream, it returns a StreamSubscription that allows you to


cancel the subscription or pause/resume it.

Example:

dart
Copy code
Stream<int> counterStream() async* {
for (var i = 0; i < 5; i++) {
yield i;
await Future.delayed(Duration(seconds: 1));
}
}

void main() {
var subscription = counterStream().listen((data) {
print(data); // Prints 0, 1, 2, 3, 4
});
Future.delayed(Duration(seconds: 3), () {
subscription.cancel(); // Cancels the subscription after 3
seconds
});
}

4. StreamTransformer

● StreamTransformer allows you to transform the data from one stream into another
type, e.g., converting Stream<int> to Stream<String>.

Example:

dart
Copy code
Stream<int> numbers() async* {
yield 1;
yield 2;
yield 3;
}

Stream<String> stringStream = numbers().transform(


StreamTransformer.fromHandlers(handleData: (data, sink) {
sink.add('Number: $data'); // Transforms data
})
);

void main() async {


await for (var value in stringStream) {
print(value); // Prints: Number: 1, Number: 2, Number: 3
}
}

5. async* and yield

● async*: Used to create a stream-producing function. It works like async but yields
multiple values over time.
● yield: Used to send values in an asynchronous generator function.

Example:

dart
Copy code
Stream<int> generateNumbers() async* {
yield 1;
yield 2;
yield 3;
}

void main() async {


await for (var num in generateNumbers()) {
print(num); // Prints: 1, 2, 3
}
}

6. Stream.listen()

● listen(): Attaches a listener to a stream, which reacts to data as it is added.

Example:

Stream<int> numbers() async* {


yield 10;
yield 20;
yield 30;
}

void main() {
numbers().listen((data) {
print(data); // Prints: 10, 20, 30
});
}
7. Exception Handling

Try-Catch
dart
Copy code
try {
int result = 12 ~/ 0;
} catch (e) {
print("Error: $e");
} finally {
print("Done!");
}

Throwing Exceptions
dart
Copy code
void checkAge(int age) {
if (age < 18) throw Exception("Too young!");
}

8. Useful Features

Spread Operator
dart
Copy code
var list = [1, 2, 3];
var extended = [...list, 4, 5];

Cascade Notation
dart
Copy code
var buffer = StringBuffer()
..write("Hello")
..write(" World!");
print(buffer.toString());

Type Checking
dart
Copy code
if (person is String) {
print("Person is a String");
}

9.Enums

● Fixed Set: Enums are used to define a group of related constants that don't change.
● Improves Code Readability: They make your code more readable by using descriptive
names for constant values.
● Type Safety: They provide better type safety by restricting values to the defined set.

Example of an Enum:
dart
Copy code
enum Day {
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday
}

Using the Enum:


dart
Copy code
void printDay(Day day) {
switch (day) {
case Day.monday:
print("Start of the week!");
break;
case Day.friday:
print("Almost weekend!");
break;
default:
print("Another day!");
}
}

void main() {
printDay(Day.monday); // Prints: Start of the week!
printDay(Day.friday); // Prints: Almost weekend!
}

Benefits:

1. Type Safety: Only allows the values defined in the enum.


2. Better Code Organization: Group related constants together.
3. Readability: Makes your code more understandable by using descriptive names.

Enum Methods:

You can access the index or name of an enum value:

enum Day {
monday,
tuesday,
wednesday,
}

void main() {
print(Day.monday.index); // Prints: 0 (index of monday)
print(Day.tuesday.toString()); // Prints: Day.tuesday (name of the
enum)
}

You might also like