Dart is a client-optimized, object-oriented, modern programming language to build apps fast for many platforms like android, iOS, web desktop, etc Dart is one of the most loved programming languages in the world.
- 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.
- Google developed Dart in 2011 as an alternative to javascript.
- /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- terminal:-> sudo nano /etc/bin :-> /opt/homebrew/bin
- brew tap dart-lang/dart
- brew install dart
They are called data types. We will learn more about data types later in this dart tutorial.
- String: For storing text value. E.g. “John” [Must be in quotes]
- int: For storing integer value. E.g. 10, -10, 8555 [Decimal is not included]
- double: For storing floating point value. E.g. 10.0, -10.2, 85.698 [Decimal is included]
- num: For storing any types of number. E.g. 10, 20.2, -20 [both int and double]
- bool: For storing true or false value. E.g. true, false [Only stores true or false values]
- var: For storing any value. E.g. ‘Bimal’, 12, ‘z’, true
- Numbers
- Strings
- Booleans
- Lists
- Maps
- Sets
- Runes
- Null
Note:The .toStringAsFixed(2) is used to round the double value upto 2 decimal places in dart. You can round to any decimal places by entering number like 2, 3, 4.
// Declaring Variables
double prize = 1130.2232323233233; // valid.
print(prize.toStringAsFixed(2));
}
- You can also create raw string in dart. Special characters won’t work here. You must write r after equal sign. EG:->String withRawString =r"The value of prize is \t $prize"; // raw String
You can convert String to int using int.parse() method. The method takes String as an argument and converts it into an integer.
You can convert int to String using toString() method. Here is example:
You can convert double to int using toInt() method.
void main() {
Set<String> weekday = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
print(weekday);
}
Note: Set doesn’t print duplicate items.
void main() {
Map<String, String> myDetails = {
'name': 'John Doe',
'address': 'USA',
'fathername': 'Soe Doe'
};
print(myDetails['name']);
}
With runes, you can find Unicode values of String. The Unicode value of a is 97, so runes give 97 as output.
void main() {
String value = "a";
print(value.runes);
}
A language is statically typed if the data type of variables is known at compile time. Its main advantage is that the compiler can quickly check the issues and detect bugs.
void main() {
var myVariable = 50; // You can also use int instead of var
myVariable = "Hello"; // this will give error
print(myVariable);
}
A language is dynamically typed if the data type of variables is known at run time.
void main() {
dynamic myVariable = 50;
myVariable = "Hello";
print(myVariable);
}
You can combine one String with another string. This is called concatenation. In Dart, you can use the + operator or use interpolation to concatenate the String. Interpolation makes it easy to read and understand the code.
- 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.
- 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.
assert(condition);
// or
assert(condition, "Your custom message");
Note: The assert(condition) method only runs in development mode. It will throw an exception only when the condition is false. If the condition is true, nothing happens. Production code ignores it.
If you remove the return type and the function name, the function is called anonymous function.
(parameterList){
// statements
}
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.
returnType functionName(parameters...) => expression;
The arrow function is used to make your code short.=> expr syntax is a shorthand for { return expr; }.
An enum is a special type that represents a fixed number of constant values. An enum is declared using the keyword enum followed by the enum’s name.Used to store a large number of constant values.
Mixins are a way of reusing the code in multiple classes. Mixins are declared using the keyword mixin followed by the mixin name. Mixin has no constructor and cannot be extended.
mixin Mixin1{
// code
}
mixin Mixin2{
// code
}
class ClassName with Mixin1, Mixin2{
// code
}
You can’t use this keyword inside factory constructor.It can be named or unnamed and called like normal constructor.
class ClassName {
factory ClassName() {
// TODO: return ClassName instance
}
factory ClassName.namedConstructor() {
// TODO: return ClassName instance
}
}
In the Dart programming language, the late keyword is used to indicate that a variable or field will be initialized at a later time. This is useful in situations where the initialization of a variable or field cannot be performed at the point of declaration, such as when the value of the variable or field depends on a value that is not yet available.
late String name;
void main() {
name = "John";
print(name);
}
- You can use if statement to check whether the variable is null or not.
- You can use ! operator, which returns null if the variable is null.
- You can use ?? operator to assign a default value if the variable is null.
void main(){
// Declaring a nullable variable by using ?
String? name;
// Assigning John to name
name = "John";
// Assigning null to name
name = null;
// Checking if name is null using if statement
if(name == null){
print("Name is null");
}
// Using ?? operator to assign a default value
String name1 = name ?? "Stranger";
print(name1);
// Using ! operator to return null if name is null
String name2 = name!;
print(name2);
}
In Synchronous programming, the program is executed line by line, one at a time. Synchronous operation means a task that needs to be solved before proceeding to the next one.
In Asynchronous programming, program execution continues to the next line without waiting to complete other work. It simply means Don't wait. It represents the task that doesn’t need to solve before proceeding to the next one.
- To Fetch Data From Internet,
- To Write Something to Database,
- To Read Data From File, and
- To Download File etc.
Note: To Perform asynchronous operations in dart you can use the Future class and the async and await keywords. We will learn Future, Async, and Await later in this guide.
Important Terms
- Synchronous operation blocks other operations from running until it completes.
- Synchronous function only perform a synchronous operation.
- Asynchronous operation allows other operations to run before it completes.
- _Asynchronous function performs at least one asynchronous operation and can also perform synchronous operations. _