02 Dart Walkthrough
02 Dart Walkthrough
Walkthrough
Jumail Bin Taliba
School of Computing, UTM
February 2020
Outline
• Introduction to Dart
• Basic syntax
• Identifiers, Data Types, Variables and Naming Convention
• String Interpolation
• Enumeration
• Control Flow Statements
• Collections
• Generics
• Functions
• Object-Oriented Programming
• Asynchronous Programming
References:
https://dart.dev/guides/language/language-tour
Download the source code
1. Open Git Bash
2. Move to the directory where you want to store the code
cd <your_working_directory>
e.g : cd d:/code/dart
5. Open the source code into VS Code (don’t forget the dot)
code .
6. Update (and download) the Dart dependencies (aka packages) for the
project.
• Open Command Palette, Ctrl Shift P
• Choose the menu Pub:Get Packages
Introduction to Dart
• Dart is an open-source, scalable programming language for
building web, server and mobile apps
1
Monday
var
Dart infers the type of day as dynamic. Thus, the variable can
hold any type of value.
const variables
myFirstFunction() my_program.dart
variableName folder
directory_name
User-defined Data Types
• Camel-case starting with Capital letter
• Each word begins with a capital letter
class Person{}
enum DayName{}
String Interpolation
Output:
Student's name: Ali age: 21
Test score: 85.0 Result: Pass
Enumeration
To define named constant values
Output:
[Days.monday, Days.tuesday, Days.wednesday, Days.thursday,
Days.friday, Days.saturday, Days.sunday]
Days.thursday
3
Today, the office is half-day
Control Flow Statements
•If-else
•For loops
•While and do-while
loops
•Break and continue
•Switch and case
If and else
For loops
While and do-while loops
break and continue
Switch and case
Collections
•Lists
•Sets
•Maps
List Collection
A list represents an array
Output:
Output:
three plus seven is ten
Advanced Operations on Collections
•Collection If
•Collection for
•Spread operator
•High-order methods
Collection if Operations
Conditionally add items to collections
Output:
Output:
Output:
Output:
10
Closure: () => int from Function 'ten': static.
10
High-order Functions vs Callbacks (1)
• Functions can also be sent as parameters to other
functions.
•
• These functions are called callback functions
Output:
Result: 3
Result: 20
Result: -3
Lambda Functions
• A callback can be directly written to the high-order function.
• This is called Lambda function (or Anonymous function, i.e. no name)
Collections and High-order Methods (1)
Collections come with several high order methods
Output:
Number 10
Number 1
Number 5
Number 7
Collections and High-order Methods (2)
Output:
Sum = 17
Number odds = 3
Generics (1)
Generics are useful for type safety
•Define functions
•Shorthand
•Function parameters
•Positional parameters
•Named parameters
•Default parameter values
Defining Functions
• Function declaration and definition are in the same place
• Dart supports hoisting. You may write the function call on top of the
function definition
Output:
Output:
Rectangle #1's area is 2.0
Rectangle #2's area is 6.0
Rectangle #3's area is 10.0
Named Parameters
• Named parameters are enclosed in { }
• To make parameters mandatory, annotate them with @required
Output:
Rectangle #1's area is 6.0
Rectangle #2's area is 15.0
Rectangle #3's area is 10.0
Object-Oriented Programming
• Define classes and create
objects
• Abstract Classes
• Constructors
• Inheritances
• Overridden Methods
• Cascade Notations
• Interfaces
• Mixins
Example problem
Abstract Classes
• An abstract class cannot be instantiated.
• It can have methods with or without definition.
Inheritance (1)
• Dart adopts single inheritance model.
• Use the keyword extends.
• Child classes inherit all members from the parent class.
• Child classes can access to parent’s members with super
Inheritance (2)
Overridden Methods
• Override a method if a child class need to have a new version of the
method.
• Annotate each method to be overridden with @override
Creating Objects (1)
• Call to the class constructor when creating objects.
• No need to explicitly write the new operator.
Creating Objects (2)
Output:
Output:
The TV is OFF
The TV is currently turned ON on channel TV3
Switching channel from TV3 to NTV7
The language is set to Bahasa Melayu
Mixin
• Mixin allows adding features to a class.
• Adding features to a class can be done with inheritance.
• So why mixin?
Shark swims
It is a shark
It swims
It is a flying fish
It swims
It flies
It is a fish
It swims
I believe I can fly
It walks
•Future
•Async / Await