Chapter 2 Introduction To Dart
Chapter 2 Introduction To Dart
Application
Development
Introduction to
Dart
Chapter Two
● Introduction to Dart
Outline ● Dart Variables and Types
● Using Dynamic Types
● Understanding control flows
● Loops in Dart
● Understanding Functions
● Data structures, collections,
Dart
● Dart is a client-optimized, object-oriented, modern programming language to build
apps fast for many platforms like android, iOS, web, and desktop, etc.
● Client optimized means optimized for crafting a beautiful user interface and high-
quality experiences.
● Google developed Dart as a programming language.
● A solid understanding of Dart is necessary to develop high-quality apps with flutter.
Features Of Dart
● Free and open-source.
● Object-oriented programming language.
● Used to develop android, iOS, web, and desktop apps fast.
● Can compile to either native code or javascript.
● Offers modern programming features like null safety and asynchronous programming.
● You can even use Dart for servers and backend
Difference Between Dart & Flutter
● Dart is client optimized, object-oriented programming language. It is popular nowadays
because of flutter. It is difficult to build complete apps only using Dart because you have to
manage many stuff yourself.
● Flutter is a framework that uses dart programming language. With the help of flutter, you
can build apps for android, iOS, web, desktop, etc. The framework contains ready-made
tools to make apps faster.
History of Dart
● Google developed Dart in 2011 as an alternative to javascript.
● Dart 1.0 was released on November 14, 2013.
● Dart 2.0 was released in August 2018.
● Dart gained popularity in recent days because of flutter.
Dart Variables and Types
A variable is “a named space in the memory” that stores values. In other words, it acts a
container for values in a program. Variable names are called identifiers. Following are the
naming rules for an identifier −
● Another way to create a multi-line string: use a triple quote with either single or
double quotation marks:
○ var s1 = '''
You can create
multi-line strings like this one.
''';
○ var s2 = """This is also a
multi-line string.""";
Built-in types
3. Booleans: To represent boolean values, Dart has a type named
bool. Only two objects have type bool: the boolean literals true
and false, which are both compile-time constants.
○ bool isMarried = true;
○ print("Married Status: $isMarried");
Comments in Dart
● Single-Line Comment: For commenting on a single line of code.
E.g. // This is a single-line comment.
● Multi-Line Comment: For commenting on multiple lines of code.
E.g. /* This is a multi-line comment. */
● Documentation Comment: For generating documentation or
reference for a project/software package. E.g. ///
Arithmetic Operators in Dart
Operator Symbol Operator Name Description
++var Pre Increment Increase Value By 1. var = var + 1 Expression value is var+1
--var Pre Decrement Decrease Value By 1. var = var - 1 Expression value is var-1
var++ Post Increment Increase Value By 1. var = var + 1 Expression value is var
var-- Post Decrement Decrease Value By 1. var = var - 1 Expression value is var
Note: ++var increases the value of operands, whereas var++ return the
actual value of operands before the increment.
Assignment Operators
Operator Type
Description
!= Used to check operand are not equal to each other and gives
Not equal to
result as boolean
! This is ’not’. return false if the result is true and vice versa
Type Test Operators
Operator
Operator Name Description
Symbol
is is Gives boolean value true if the object has a specific type
is! is not Gives boolean value false if the object has a specific type
Properties of String
● codeUnits: Returns an unmodifiable list of the UTF-16 code units of this string.
● isEmpty: Returns true if this string is empty.
● isNotEmpty: Returns false if this string is empty.
● length: Returns the length of the string including space, tab, and newline characters.
Methods Of String
● toLowerCase(): Converts all characters in this string to lowercase.
● toUpperCase(): Converts all characters in this string to uppercase.
● trim(): Returns the string without any leading and trailing whitespace.
● compareTo(): Compares this object to another.
● replaceAll(): Replaces all substrings that match the specified pattern with a given value.
● split(): Splits the string at matches of the specified delimiter and returns a list of
substrings.
● toString(): Returns a string representation of this object.
● substring(): Returns the text from any position you want.
● codeUnitAt(): Returns the 16-bit UTF-16 code unit at the given index.
Types Of Condition
● If Condition
● If-Else Condition
● If-Else-If Condition
● Switch case
Ternary Operator in Dart
● The ternary operator is like if-else statement. This is a one-liner
replacement for the if-else statement. It is used to write a conditional
expression, where based on the result of a boolean condition, one of
the two values is selected.
// function body
}
Function In Dart
● Return type: It tells you the function output type. It can be void, String, int,
double, etc. If the function doesn’t return anything, you can use void as the
return type.
● Function Name: You can name functions by almost any name. Always follow a
lowerCamelCase naming convention like void printName().
● Parameters: Parameters are the input to the function, which you can write
inside the bracket (). Always follow a lowerCamelCase naming convention for
your function parameter.
Anonymous Function In Dart
● Every function needs a name. If you remove the return type and the function
name, the function is called anonymous function.
● Syntax
(parameterList){
// statements
}
Arrow Function In Dart
● Dart has a special syntax for the function body, which is only one line. The
arrow function is represented by => symbol. It is a shorthand syntax for any
function that has only one expression.
● Syntax
// String List
// Mixed List
● Growable List [Mostly Used] → A List defined without a specified length is called Growable List.
var list1 = [210,21,22,33,44,55];
List Properties In Dart
● first: It returns the first element in the List.
● last: It returns the last element in the List.
● isEmpty: It returns true if the List is empty and false if the List is not empty.
● isNotEmpty: It returns true if the List is not empty and false if the List is empty.
● length: It returns the length of the List.
● reversed: It returns a List in reverse order.
● Single: It is used to check if the List has only one element and returns it.
List in Dart
● Adding Item To List
● Replace Range Of List
● Removing List Elements
● Loops In List
● Combine Two Or More List In Dart
● Conditions In List
● Where In List Dart
Set In Dart
● Set is a unique collection of items.
● You cannot store duplicate values in the Set.
● It is unordered, so it can be faster than lists while working with a large amount of data.
Set is useful when you need to store unique values without considering the order of
the input.
● It is represented by Curley Braces{}.
● Note: The list allows you to add duplicate items, but the Set doesn’t allow it.
Set <variable_type> variable_name = {};
Map In Dart
● In a Map, data is stored as keys and values. In Map, each key must be unique. They are
similar to HashMaps and Dictionaries in other languages.
Map<String, String> countryCapital = {
'China': 'Beijing'
};
● You can find the value of Map from its key. Here we are printing Washington, D.C. by its key,
i.e., USA.
Map Properties In Dart
Properties Work
Properties Work
End