0% found this document useful (0 votes)
6 views

Dart Programming language.docx

Uploaded by

livy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Dart Programming language.docx

Uploaded by

livy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Dart Programming language

Introduction to Dart

Dart is a client-optimized programming language developed by Google. It's designed for


building fast apps on multiple platforms, including mobile, web, desktop, and backend.
The primary purpose of Dart is to offer high performance and ease of use for UI development,
especially when paired with frameworks like Flutter.

Key Features of Dart


⮚ Strongly Typed: Dart is a statically typed language, which means the type of every
variable is known at compile-time, leading to fewer errors during runtime.
⮚ Null Safety: With Dart 2.12 and later versions, Dart offers null safety, helping
developers avoid null reference errors by making nullable and non-nullable types
explicit.
⮚ Ahead-of-Time (AOT) Compilation: Dart compiles to native code for mobile and
desktop applications (ensuring high performance) and can also compile to JavaScript for
web applications.
⮚ Hot Reload: When used with Flutter, Dart supports Hot Reload, allowing developers to
see the effects of code changes instantly without losing the app’s state.

Syntax Highlights

⮚ Variables and Types: Dart uses var for declaring variables whose type is inferred by the
compiler. Alternatively, specific types like int, double, String, or bool can be declared
explicitly.
⮚ Functions: Dart functions can have both positional and named parameters (useful for
making APIs clearer), and they can return values using => for single expressions.
⮚ Classes and Objects: Dart is fully object-oriented, supporting classes, inheritance,
interfaces, and abstract classes.
⮚ Asynchronous Programming: Dart has built-in support for asynchronous programming
using Future, async, and await. This makes it easy to perform tasks like HTTP requests
without blocking the main thread.

Dart Ecosystem
⮚ Flutter Framework: Dart is the main language used with the Flutter framework, which
allows for cross-platform mobile and web development.
⮚ Pub: Dart uses a package manager called Pub to handle libraries and dependencies. It
provides access to a rich ecosystem of reusable packages.
⮚ DartPad: An online tool for experimenting with Dart code in real time without requiring
installation. Useful for quick prototyping.
Use Cases of Dart
⮚ Mobile Apps: With Flutter, Dart is used extensively for building iOS and Android
applications using a single codebase.
⮚ Web Apps: Dart can compile to JavaScript, enabling it to run in web browsers.
⮚ Backend: Dart can be used for server-side applications, such as REST APIs, using
frameworks like Aqueduct or Shelf.
⮚ Desktop Applications: With Dart’s AOT compilation and Flutter, you can build native
desktop apps for Windows, macOS, and Linux.
Pros and Cons of Dart
Pros

⮚ Fast development: With Hot Reload and fast AOT compilation.


⮚ Cross-platform: One codebase for web, mobile, and desktop.
⮚ UI-first language: Built for creating visually appealing user interfaces.
⮚ Easy to learn: Syntax is familiar, especially for those with experience in C-style
languages (e.g., Java, C#, JavaScript).

Cons

⮚ Smaller community compared to languages like JavaScript or Python.


⮚ Limited backend adoption compared to languages like Node.js or Python.
⮚ Still growing for desktop app development.

DartPad Editor
Dartpad is a browser based for playing with dart. It is a tool that help you start the language.
Website: https://dartpad.dev
Variables in dart
In Dart, variables are used to store data that can be accessed and manipulated in a program.
Here's a detailed overview of how variables work in Dart:
Variable Declaration
Variables in Dart can be declared in several ways. You can either explicitly declare the type of
the variable or allow Dart to infer the type.
Using var Keyword (Type Inference)
Dart can infer the type of a variable based on the assigned value.
var name = ‘Jerry’; // Dart infers that name is a String
var age = 30; // Dart infers that age is an int

Once the type is inferred, it cannot change. For example, if you assign a string to var, you
cannot later assign an integer to it.
name = 123; // Error: A value of type 'int' can't be assigned to a variable of type 'String'.

Final and Const Variables


Dart provides final and const keywords to declare variables that cannot be changed after they
are initialized.
final Keyword
A final variable can be assigned only once. After it’s set, it cannot be modified, but the value
can be assigned at runtime (not necessarily at compile time).

final name = 'Jerry';


name = 'Adu'; // Error: Cannot change the value of a final variable.

const Keyword
A const variable is a compile-time constant, meaning its value must be known at compile time
and it cannot be modified. It is more strict than final.
Dynamic Variables
Dart supports the dynamic type, which allows a variable to change its type at runtime. It
provides more flexibility but reduces type safety.

dynamic value = 'Hello';


print(value);
value = 123;
print(value);
Functions in dart
In Dart, functions are fundamental building blocks that allow you to group code into reusable
units. A function can take in parameters, perform operations, and return a value. Functions in
Dart are flexible, with features like optional parameters, named parameters, higher-order
functions, and more.

Here's overview of functions in Dart:


Basic Function Syntax
A basic Dart function is defined using the following syntax:

returnType functionName(parameterList) {
// function body
return value;
}
Example
int add(int a, int b) {
return a + b;
}
void main() {
int result = add(3, 4);
print(result); // Output: 7
}

Return Type: The function can have an explicit return type, like int, String, bool, etc. If no
return value is required, use void.

Parameters: Functions can take zero or more parameters.

Return Statement: A return statement is used to return a value from the function.

You might also like