0% found this document useful (0 votes)
1 views12 pages

Flutter Basics

Dart is an open-source programming language developed by Google, primarily used for building mobile, web, and server applications, especially with the Flutter framework. The tutorial covers Dart's syntax, data types, control flow, object-oriented programming, and advanced concepts, providing a comprehensive guide for developers. Key features include fast performance, ease of learning, and the ability to use a single codebase across multiple platforms.
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)
1 views12 pages

Flutter Basics

Dart is an open-source programming language developed by Google, primarily used for building mobile, web, and server applications, especially with the Flutter framework. The tutorial covers Dart's syntax, data types, control flow, object-oriented programming, and advanced concepts, providing a comprehensive guide for developers. Key features include fast performance, ease of learning, and the ability to use a single codebase across multiple platforms.
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/ 12

DART

Dart is an open-source general-purpose programming language developed by Google. It supports


application development on both the client and server side. However, it is widely used for the
development of Android apps, iOS apps, IoT(Internet of Things), and web applications using
the Flutter Framework.
This Dart tutorial will help you get up to speed. We have covered everything you need to know, from
setting up Dart to using its advanced features. By the end, you'll be ready to create fast, efficient
applications. Let's dive in and see how Dart can make your coding projects easier and more fun!
Syntactically, Dart bears a strong resemblance to Java, C, and JavaScript. It is a dynamic object-
oriented language with closure and lexical scope. The Dart language was released in 2011 but came
into popularity after 2015 with Dart 2.0.

Why Use Dart

 Fast & Smooth: Dart compiles to native code for speedy performance, ideal for mobile apps.
 Easy to Learn: Similar to familiar languages like Java or Javascript, making it approachable for
new developers.
 Flutter Power: Dart is the heart of Flutter, a popular framework for building beautiful and
functional mobile apps.
 One Code, Many Places: Develop for mobile, web, and even desktop with a single codebase
(primarily with Flutter).

Dart Basics

In this section, you will explore into the fundamental concepts of Dart programming. Dart is a versatile
language known for its simplicity and efficiency in developing applications for various platforms.
You'll start by understanding basic syntax and data types, essential for writing clear and concise code.
Explore how Dart handles variables, functions, and control flow statements, laying a solid foundation
for building more complex applications.
 Introduction to Dart Programming Language
 Dart - SDK Installation
 Dart - Comments
 Dart - Variables
 Dart - Operators
 Dart - Standard Input Output

Data Types in Dart

Data Types are used for defining the type of data that a variable can store. We can store multiple types
of data in Dart as mentioned below:

 Dart - Data Types


 Dart - Numbers
 Dart - Strings
 Dart - List
 Dart - Sets
 Dart - Map
 Queues in Dart
 Dart - Enums
Control Flow in Dart

Control Flow in Programming is refers to the order in which set of instructions or statements that are
executed or evaluated. It provides flexibility in Decision making and makes code more user friendly.
 Dart - If-Else Statements
 Dart - Switch Case Statements
 Dart - Loops
 Dart - Loop Control Statements
 Labels in Dart

Dart Key Functions

Functions are used for making our Dart Programs more organised and efficient. Here, we will check
how to build functions and learn about few inbuilt functions in Dart which can lower some of the loads
from us.
 Dart - Function
 Dart - Types of Functions
 Dart - Anonymous Function
 Dart - main() Function
 Dart - Common Collection Methods
 Dart - exit() function
 Getter and Setter Methods in Dart

Object-Oriented Programming(OOPS) in Dart

OOPS is important part of Dart Programming Language let us learn topics from encapsulation to
inheritance, polymorphism, abstract classes, and iterators, we’ll cover the essential concepts that
empower you to build modular, reusable, and scalable code.
 Dart - Classes & Objects
 Dart - Constructors
 Dart - Super Constructor
 Dart - this Keyword
 Dart - static Keyword
 Dart - super Keyword
 Dart - Const And Final Keyword
 Dart - Inheritance
 Dart - Methods
 Dart - Method Overloading
 Dart - Getters & Setters
 Dart - Abstract Classes
 Dart - Builder Class
 Concept of Callable Classes in Dart
 Dart - Interfaces
 Dart - extends Vs with Vs implements

Dart Utilities

In this section, we explore Dart utilities that enhance development efficiency. Dart offers a robust set of
utility functions and libraries that simplify common tasks like handling collections, working with dates
and times, and managing asynchronous operations. These utilities empower developers to write cleaner,
more concise code, speeding up the development process and improving code maintainability.
 Dart - Date and Time
 Using await async in Dart
 Data Enumeration in Dart
 Dart - Type System
 Generators in Dart

Dart Programs

Dart programs are structured using clear syntax and support object-oriented principles, making them
versatile for both beginners and experienced developers alike. You'll learn how to write and execute
Dart code, understand variables, functions, and control flow structures essential for building functional
and efficient applications.
 How to Combine Lists in Dart?
 Dart - Finding Minimum and Maximum Value in a List
 Dart - Splitting of String
 How to Append or Concatenate Strings in Dart?
 How to Find the Length of a String in Dart?
 Dart - Sort a List
 How to convert a lowercase string to an uppercase string?
 How to convert all characters of a string to lowercase?
 How to Replace a Substring of a String in Dart?
 How to Check String is Empty in Dart?

Advance Concepts

In the advance concept section you will explore advanced concepts of Dart in depth. These concepts
build upon the foundational knowledge you've gained, allowing you to tackle more complex challenges
in Dart programming. From mastering asynchronous programming and working with streams to
implementing advanced design patterns like dependency injection and state management, these topics
are essential for developing scalable and efficient applications.
 Exception Handling in Dart
 Assert Statements in Dart
 Fallthrough Condition in Dart
 Concept of Isolates in Dart
 Dart - Typedef
 Dart - URIs
 Dart - Collections
 Dart - Packages
 Dart - Generators
 Dart - Callable Classes
 Dart - Isolates
 Dart - Async
 Dart – String codeUnits Property
 Dart - HTML DOM

Dart - Data Types


Like other languages (C, C++, Java), whenever a variable is created, each variable has an associated data
type. In Dart language, there are the types of values that can be represented and manipulated in a
programming language.
In this article, we will learn about Dart Programming Language Data Types.

Data Types in Dart


The data type classification is as given below:
Data Type Keyword Description

int, double, num,


Number Numbers in Dart are used to represent numeric literals
BigInt

Strings String Strings represent a sequence of characters

Booleans bool It represents Boolean values true and false

An ordered list of elements (like arrays in other


Lists List
languages)

Sets Set A list of distinct elements (unsorted)

Maps Map Collection of key-value pairs where keys are unique

Used for manipulating Unicode characters


Runes Runes
(although characters API is now preferred)

It is an identifier symbol, which is primarily utilized in


Symbols Symbol
reflection and debugging

Null Null Represents the lack of a value

1. Number (int, double, num, BigInt)


The number in Dart Programming is the data type that is used to hold the numeric value. Dart numbers
can be classified as:
 int: It is used to represent whole numbers (64-bit Max).
 double: It is used to represent 64-bit precise floating-point numbers.
 num: It is an inherited data type of the int and double types.
 BigInt: For very large integers that exceed int limits, use BigInt.
Declaring Integer (int)
// Method 1: Declaring an integer variable
int age = 25;

// Method 2: Nullable integer declaration


int? count;

// Method 3: Using 'var' keyword (automatically detects type)


// Dart infers it as int
var year = 2024;
Declaring Decimal Numbers (double)
// Method 1: Explicit declaration
double pi = 3.1415;

// Method 2: Nullable double


double? percentage;

// Method 3: Using 'var' keyword


// Dart infers it as double
var temperature = 36.6;
Using num for Both Integer and Decimal Values
num value = 10;

// Allowed because 'num' supports both int and double


value = 10.5;
Declaring Large Integers Using BigInt
BigInt bigNumber = BigInt.parse('987654321098765432109876543210');
Below is the implementation of Numbers in Dart:
// Dart program to demonstrate
// Number Data Type
void main() {

// Declare an integer
int num1 = 2;

// Declare a double value


double num2 = 1.5;

// Print the integer and


// double values
print("$num1");
print("$num2");

// Perform addition
// (int + double results in a double)
var sum = num1 + num2;

// Print the sum of


// num1 and num2
print("Sum = $sum");
}
Output:
2
1.5
Sum = 3.34
2. Strings (String )
It used to represent a sequence of characters. It is a sequence of UTF-16 code units. The keyword
string is used to represent string literals. String values are embedded in either single or double-
quotes.
Declaring String in Dart:
String str_name;
Below is the implementation of String data type in Dart:
// Dart program to demonstrate
// String Data Type

void main() {

// Declare and initialize a string


String string = "Geeks for Geeks";

// Declare two separate string variables


String str = 'Coding is ';
String str1 = 'Fun';

// Print the string variable


print(string);

// Concatenate and print the two strings


print(str + str1);

// Output: Coding is Fun


}

Output:
GeeksforGeeks
Coding is Fun

3. Boolean (bool)
It represents Boolean values true and false. The keyword bool is used to represent a Boolean literal in
dart.
Declaring Boolean in Dart
bool var_name;
Below is the implementation of Boolean in Dart:
// Dart program to demonstrate
// Boolean Data Type

void main() {

// Declare a boolean variable


// with a true value
bool val1 = true;
// Declare two string variables
String str = 'Coding is ';
String str1 = 'Fun';

// Compare the two strings


// (returns false)
bool val2 = (str == str1);

// Print boolean values


print(val1); // Output: true
print(val2); // Output: false
}

Output:
true
false

4. Lists (List)
List data type is similar to arrays in other programming languages. A list is used to represent a
collection of objects. It is an ordered group of objects.
Declaring List in Dart
There are multiple methods to declare List in Dart as mentioned below:
1. Variable Size List
// Empty growable list (preferred way)
List<int> var_name1 = [];

// Alternative: Declaring a list but not initializing (null safety requires ? if uninitialized)
List<int>? var_name2;

// Another way using List constructor


List<int> var_name3 = List.empty(growable: true);
2. Fixed Size List
Fixed Size doesn't mean that we can't change the size of List, but we have predefined that the List has
this much elements while declaring.
// Creates a fixed-size list with default values (0 in this case)
List<int> var_name1 = List<int>.filled(5, 0);

// Generates a fixed-size list where each element is `index * 2`


List<int> var_name2 = List<int>.generate(5, (index) => index * 2);
Below is the implementation of List in Dart:
void main() {

// Creating a fixed-size list with


// 3 elements, each initialized to "default"
List<String> gfg = List<String>.filled(3, "default");

// Updating elements in the list


// (modifying values is allowed,
// but resizing is not)
gfg[0] = 'Geeks';
gfg[1] = 'For';
gfg[2] = 'Geeks';

// Printing the entire list to


// verify the changes
print(gfg); // Output: [Geeks, For, Geeks]

// Accessing and printing a


// specific element by its index
print(gfg[0]); // Output: Geeks
}

Output:
[Geeks, For, Geeks]
Geeks

5. Sets (Set)
A Set is an unordered collection of unique elements.
Declaring Set in Dart
// Method 1: Using curly braces
Set<int> uniqueNumbers = {1, 2, 3, 3, 4};

// Method 2: Using Set constructor


Set<String> cities = Set();
cities.add("New York");
cities.add("London");
Below is the implementation of Set in Dart:
void main() {

// Declaring a Set of Strings containing country names


// Sets in Dart do not allow duplicate values
Set<String> countries = {"USA", "India", "USA"};

// Printing the set


// Since sets store only unique values, "USA" appears only once
print(countries);

// Output: {USA, India}


}

Output:
{USA, India}

6. Maps (Map)
The Map object is a key and value pair. Keys and values on a map may be of any type. It is a dynamic
collection.
Declaring Map in Dart
While Declaring Map there can be only two cases one where declared Map is empty and another where
declared Map contains elements in it. Both Cases are mentioned below:
1. Declaring Empty Map
// Method 1: Nullable Map declaration (can be assigned later)
Map? mapName;

// Method 2: Explicitly specifying key-value data types


Map<String, int>? mapName2;

// Method 3: Using the 'var' keyword with Map constructor


// Defaults to Map<dynamic, dynamic>
var mapName3 = Map();

// Method 4: Using a type-safe empty map


// Recommended approach
Map<String, int> mapName4 = {};
2. Declaring Map with Elements inside it.
/// Method 1: Using curly braces (preferred way)
Map<String, String> myMap = {
"First": "Geeks",
"Second": "For",
"Third": "Geeks",
};

// Method 2: Using Map constructor


Map<String, int> mapExample = Map();
mapExample["One"] = 1;
mapExample["Two"] = 2;

// Method 3: Using 'var' with implicit typing


var anotherMap = {
"A": 10,
"B": 20,
"C": 30,
};
Below is the implementation of Map in Dart:
void main() {

// Creating a map using the preferred syntax


Map<String, String> gfg = {};

// Adding key-value pairs


gfg['First'] = 'Geeks';
gfg['Second'] = 'For';
gfg['Third'] = 'Geeks';

// Printing the map


print(gfg);
}
Output:

{First: Geeks, Second: For, Third: Geeks}

7. Runes (Runes)
Dart utilizes Runes to represent Unicode characters that fall outside the standard ASCII character set.
Since Dart strings are encoded in UTF-16, certain special characters such as emojis and non-English
scripts must be expressed using runes.
Declaring Runes in Dart
String heart = '\u2665'; // Unicode for ♥
Below is the implementation of Runes in Dart:
void main() {
// Unicode for heart symbol (♥)
String heart = '\u2665';

// Unicode for smiley face ( )


String smiley = '\u263A';

// Unicode for star symbol (★)


String star = '\u2605';

// Unicode for musical note (♫)


String musicNote = '\u266B';

// Printing all Unicode symbols


print(heart); // Output: ♥

print(smiley); // Output:
print(star); // Output: ★
print(musicNote); // Output: ♫

Output:


8. Symbols (Symbol)
A Symbol in Dart is an immutable identifier that represents variable names, method names, or metadata
at runtime. Symbols are primarily used in reflection and are helpful for dynamic programming.
Declaring Symbols in Dart
Symbol sym1 = #mySymbol;
Symbol sym2 = Symbol("anotherSymbol");
Below is the implementation of Symbols in Dart:
void main() {

// Declaring a Symbol
// using the # syntax
Symbol sym1 = #dart;

// Declaring another Symbol


// with a different identifier
Symbol sym2 = #flutter;

// Printing the Symbols


print(sym1); // Output: Symbol("dart")
print(sym2); // Output: Symbol("flutter")

// Using Symbol in a map


// (useful for metadata or reflection)
Map<Symbol, String> symbolMap = {
#language: "Dart",
#framework: "Flutter",
};

// Printing values using Symbols as keys


print(symbolMap[#language]); // Output: Dart
print(symbolMap[#framework]); // Output: Flutter
}

Output:
Symbol("dart")
Symbol("flutter")
Dart
Flutter

9. Null (Null)
Dart uses null to indicate the absence of a value. It has null safety, which means variables must be
either nullable (?) or initialized before they can be used.
Declaring Null in Dart
String? name; // Can be null
Below is the implementation of Null in Dart:
void main() {

// Nullable variables (can be assigned null)


String? name;
int? age;

// Assigning values
name = "GFG";
age = null;
// Checking for null values
// using null-aware operators
print(name ?? "Unknown"); // Output: Vinay
print(age ?? "No age provided"); // Output: No age provided

// Using null-aware access (?.)


// and null assertion (!)
int? length = name?.length;

print(length);

// Output: 5
}

Output:
GFG
No age provided
3
Note: If the type of a variable is not specified, the variable’s type is dynamic. The dynamic keyword is
used as a type annotation explicitly.

You might also like