Any Dart program, whether it be a script or a Flutter application, must specify the main function.
The file that is regarded as the "entry point" for your program must have this function, which instructs Dart where the program begins. This will often be in a file called main.dart.
The stdn.readLineSync() method in the Dart programming language allows you to read standard input from the user via the console. You must import the dart:io library from the Dart libraries in order to accept input from the console.
Taking a string input from user:
The ouput can simply printed using the command print().
Input:
Output:
Dart String is a character sequence. It is used to store the value of the text. You can use single or double quotations to enclose the string. The triple-quotes can be used to generate the multiline string. Because strings are immutable, you cannot change them after they have been created.
The String keyword in Dart can be used to declare a string. Below is a list of the string declaration's syntax.
The + or += operator is used to merge the two string. The example is given below.
Output:
The string interpolation is a technique to manipulate the string and produce a new string by adding another value. String interpolation is done using the ${expression}. The corresponding values are used to replace the expressions. Let's clarify with the next example.
Escape sequences can be used to insert tab, new line, and etc into an output stream.
Here is an example of \n starting a new line and \t adding a tab in between the string:
Output
A raw string can be used to avoid escaping only backslashes and dollars.
Output:
Property/Method | Description |
---|---|
isEmpty | If the string is empty, it returns true. |
Length | It returns the length of the string including whitespace. |
toLowerCase() | It converts all characters of the given string in lowercase. |
toUpperCase() | It converts all characters of the given string in uppercase. |
trim() | It eliminates all whitespace from the given string. |
replaceAll() | It replaces all substring that matches the specified pattern with a given string. |
isEmpty | If the string is empty, it returns true. |
split() | It splits the string at matches of the specified delimiter and returns the list of the substring. |
substring() | It returns the substring from start index, inclusive to end index. |
toString() | It returns the string representation of the given object. |
contains() | To check if a string contains other string. |
The Number is the data type that is used to hold the numeric value. In Dart, It can be two types:
The term "integer" refers to a set of whole integers that may be expressed without a fractional component. For example - 24, 40, -3565, 0, etc. You can have a signed or unsigned integer. The integer value is represented as a range of non-decimal values, from -263 to 263. In Dart, integer values are declared using the int keyword.
Rules for the integer value
Let's look at the example below:
The Double number are the numbers that can be written with floating-point numbers or number with larger decimal points. The double keyword is used to declare Double value in Dart.
Example:
The parse() function converts the numeric string to the number.
Let's see the example below:
Output:
Basic Operations: We can use (+, -, /. *) for simple operations.
Output:
Increment & decrement: We can use (++, –, +=, -=) for increment and decrement as follow:
Output:
Another example:
Output:
Remider : We can use % to returns the remainder of the division.
Output:
Truncating division: Truncating division is division where a fractional result is converted to an integer by rounding towards zero. We use ~/ for this operation.
Output:
Arithmetic Operators Table
Operators | Meaning |
---|---|
+ | Add |
− | Subtract |
* | Multiply |
/ | Divide |
~/ | Divide, returning an integer result |
% | Get the remainder of an integer division (modulo) |
++ | Increment |
- - | Decrement |
Properties | Description |
---|---|
hashcode | It returns the hash code of the given number. |
isFinite | If the given number is finite, then it returns true. |
isInfinite | If the number infinite it returns true. |
isNan | If the number is non-negative then it returns true. |
isNegative | If the number is negative then it returns true. |
sign | It returns -1, 0, or 1 depending upon the sign of the given number. |
isEven | If the given number is an even then it returns true. |
isOdd | If the given number is odd then it returns true. |
Method | Description |
---|---|
abs() | It gives the absolute value of the given number. |
ceil() | It gives the ceiling value of the given number. |
floor() | It gives the floor value of the given number. |
compareTo() | It compares the value with other number. |
remainder() | It gives the truncated remainder after dividing the two numbers. |
round() | It returns the round of the number. |
toDouble() | It gives the double equivalent representation of the number. |
toInt() | Returns the integer equivalent representation of the number. |
toString() | Returns the String equivalent representation of the number |
truncate() | Returns the integer after discarding fraction digits. |
Only the values true and false are allowed by the Boolean data type in DART. A Boolean literal is declared by the keyword bool in DART.
The following is the syntax for defining a Boolean variable in DART:
Other examples
Output
To understand this concept first we need to know the difference between static and dynamic. The statically typed languages are those which check the variable types at compile time like C, C++, Swift, Kotlin. In static programming languages, data types are known and checked at compile time while in Dynamic programming languages data types are checked during run-time. Languages like JS, Python R.
Now let’s compare dart with the static and dynamic.
In our first test if we define a variable with an unknown type or without a dart type dart will throw an error at compile time before running the code it means dart checks the variable types at compile-time So we can say Dart is a statically typed programming language where variable types are known at compile-time in dart.
With dart code, you can enjoy the static and dynamic typed both at the same time. Dart offer a dynamic variable that is checked at runtime, you can store any type of data in dynamic at runtime.
Dart List is similar to an array, which is the ordered collection of the objects. The most well-known and often utilized collection in any other programming language is the array. The JavaScript array literals resemble the Dart list.
The list's declaration syntax is shown below.
All components must be kept inside the square bracket ([]) and separated by commas to form the Dart list (,).
Elements can be accessed by specify the index of the element or by conditional property.
Output
You may use this function to get a list containing elements that satisfy a condition.
Output
Return the first element that statify the condition.
Output
Dart provides four methods which are used to insert the elements into the lists. These methods are given below.
This method is used to insert the specified value at the end of the list. It can add one element at a time and returns the modified list object.
Output
This method is used to insert the multiple values to the given list. Each value is separated by the commas and enclosed with a square bracket ([]). The syntax is given below.
Output
The insert() method provides the facility to insert an element at specified index position. We can specify the index position for the value to be inserted in the list. The syntax is given below.
Output
The insertAll() function is used to insert the multiple value at the specified index position. It accepts index position and list of values as an argument. The syntax is given below.
Output
The Dart provides the facility to update the list and we can modify the list by simply accessing its element and assign it a new value. The syntax is given below.
Output
The Dart provides following functions to remove the list elements.
It removes one element at a time from the given list. It accepts element as an argument. It removes the first occurrence of the specified element in the list if there are multiple same elements. The syntax is given below.
Output
It removes an element from the specified index position and returns it. The syntax is given below.
Output
The removeLast() method is used to remove the last element from the given list. The syntax is given below.
Output
This method removes the item within the specified range. It accepts two arguments - start index and end index. It eliminates all element which lies in between the specified range. The syntax is given below.
Output
The Dart List can be iterated using the forEach method. Let's have a look at the following example.
Properties | Explanations |
---|---|
first | It is used to get the first element in the given set. |
isEmpty | If the set does not contain any element, it returns true. |
isNotEmpty | If the set contains at least one element, it returns true. |
length | It returns the length of the given set. |
last | It is used to get the last element in the given set. |
It is the same as an list, but it doesn't allow storing the duplicate values. The set must contain unique values.
Set can be converted to a List, and vice versa using toList() and toSet() methods.
Dart Map is an object that stores data in the form of a key-value pair. Each value is associated with its key, and it is used to access its corresponding value. Both keys and values can be any type. In Dart Map, each key must be unique, but the same value can occur multiple times. The Map representation is quite similar to Python Dictionary. The Map can be declared by using curly braces {} ,and each key-value pair is separated by the commas(,). The value of the key can be accessed by using a square bracket([]).
Dart Map can be defined as follow
example 1
Output
example 2:
Output
Property | Description |
---|---|
Keys | Returns an iterable object representing keys |
Values | Returns an iterable object representing values |
Length | Returns the size of the Map |
isEmpty | Returns true if the Map is an empty Map |
isNotEmpty | Returns true if the Map is an empty Map |
Method | Description |
---|---|
addAll() | Adds all key-value pairs of other to this map. |
clear() | Removes all pairs from the map. |
remove() | Removes key and its associated value, if present, from the map. |
forEach() | Applies f to each key-value pair of the map. |
To regulate the flow of a Dart program, control statements or a control flow statements are used. In all programming languages, these statements have an important role in determining whether or not other statements will be executed. In most cases, the code statement is executed sequentially. Depending on the given condition, we could need to execute or skip a group of statements, jump to another statement, or repeat the execution of the statements.
A control statement in Dart enables a smooth program flow. A Dart program can be altered, redirected, or repeated based on the application logic by using the control flow statements.
Dart's control flow statements may be divided into the three categories listed below:
Dart provides following types of Decision-making statement.
Dart looping statements are used to execute the block of code multiple-times for the given number of time until it matches the given condition.
Dart provides following types of the looping statements:
Jump statements are used to jump from another statement, or we can say that it transfers the execution to another statement from the c
continue
Output
break
Output
An enumeration is a set of values called as elements, members, etc. This is essential when we carried out the operation with the limited set of values available for variable. For example - you can think of the days of the month can only be one of the seven days - Sun, Mon, Tue, Wed, Thur, Fri, Sat.
Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc.
Output
When you opt into null safety, types in your code are non-nullable by default, meaning that variables can’t contain null unless you say they can. With null safety, your runtime null-dereference errors turn into edit-time analysis errors.
With null safety, all of the variables in the following code are non-nullable:
To indicate that a variable might have the value null, just add ? to its type declaration:
We can use (!) on a nullable value to tell the compiler that the variable will not be null in this line. you should use it only when you 100% sure that the variable will not be null.
Dart offers some handy operators for dealing with values that might be null. One is the ??= assignment operator, which assigns a value to a variable only if that variable is currently null:
Another null-aware operator is ??, which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right:
To guard access to a property or method of an object that might be null, put a question mark (?) before the dot (.):
The preceding code is equivalent to the following:
You can chain multiple uses of ?. together in a single expression:
A dart function is a collection of codes that when put together, carry out a certain operation. It is used to divide the lengthy code into smaller modules so that it may be reused as required. Programs using functions are easier to read and debug. It strengthens the modular approach and increases code reuse.
Let's say we need to run operations repeatedly while the user inputs data in a basic calculator software. Each calculator operator can have a distinct function written for it. We may do repetitive operations like adding, subtracting, multiplying, and dividing without continually writing code by using the functions. By calling, we may utilize the functions more than once.
The function gives you the ability to run a code several times with various values. A function can be called anytime as its parameter and returns some value to where it called.
Let's understand the general syntax of the defining function:
Output
It is not neccessary to return data. We can use methods to do something without return.
example:
Output
Optional parameters are parameters which don't have to be specified when calling given function. Optional parameters must be declared after required parameters. Additionally, optional parameters can have a default value, which is used once the function invocation doesn't specify it.
Output
We define named parameters by listing them between curly brackets {}, e.g., {int a, int b, …}.
Named parameters are optional unless they’re explicitly marked as required. When calling a function, you can specify named arguments using paramName: value.
Here is the same sum function, but this time, we write it with named parameters.
Output
Asynchronous operations let your program complete work while waiting for another operation to finish. Here are some common asynchronous operations:
Fetching data over a network.
Writing to a database.
Reading data from a file.
Such asynchronous computations usually provide their result as a Future. To interact with these asynchronous results, you can use the async and await keywords.
To make a method asynchronous, we should add:
1- Change the return type (if exist) Future<Object> instead of Object.
2- Add async before the curly bracket.
3- add await before the asynchronous function.
Problems that occur while a program is being executed are referred to as exceptions. When an exception occurs, the program's normal flow is interrupted, and the program or application terminates unexpectedly.
The try block embeds code that might possibly result in an exception. The on block is used when the exception type needs to be specified. The catch block is used when the handler needs the exception object.
Syntax
You sould use (on) or (catch) or both. (finally) is optional.
example 1:
example 2:
The follwoing example shows how to throw an exception:
Dart is an object-oriented language. It supports object-oriented programming features like classes, interfaces, etc. A class encapsulates data for the object. Dart gives built-in support for this concept called class.
Use the class keyword to declare a class in Dart. A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. The syntax for the same is given below:
I will add the rest as soon as I can.
here is the example that we discusss in the last meeting.