Dart Programming language.docx
Dart Programming language.docx
Introduction to Dart
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
Cons
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'.
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.
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.
Return Statement: A return statement is used to return a value from the function.