Overview of Dart | Features and Functions!
Today, I will introduce Dart, the programming language used in Flutter!
Dart was developed by Google as an alternative to JavaScript and was first introduced in 2011.
It incorporates features from modern programming languages like Java, Python, and JavaScript, making it relatively easy to learn for those already familiar with these languages.
Initially, Dart remained a minor language, but after being adopted as the official programming language for Flutter, it regained attention and became widely used.
Key Features of Dart
1. Optimized for UI Development
- Features like Null Safety and collection spread operators enhance UI development
- Supports asynchronous and event-driven programming for UI responsiveness
2. High Productivity
- Supports profiling, logging, and debugging tools
- Hot Reload enables real-time updates
3. Fast Execution on All Platforms
- Can be compiled into native machine code for high performance
- Can be optimized for JavaScript execution
Additional Features of Dart
- Multi-paradigm language (includes object-oriented programming)
- Statically typed with type inference
- Supports Null Safety
- All classes inherit from the Object class
- Includes both expressions (returning values) and statements (not returning values)
- The compiler reports errors and warnings at compile-time and runtime
- Functions are first-class objects, meaning they can be assigned to variables and passed as arguments
Dart’s Functionalities
Operators and Data Types
Dart provides a variety of operators and data types, including:
- Operators
- Arithmetic:
+,-,*,/,% - Increment & Decrement:
++,-- - Logical:
&&,||,! - Null-aware:
?? - Compound Assignment:
+=,-=, etc. - Type Casting & Checking:
as,is - Cascade Notation:
..(allows method chaining)
- Arithmetic:
var list = List<int>();
list.add(1);
list.add(2);
// Using cascade notation
var list2 = List<int>()..add(1)..add(2);
- Spread Operator (
...)- Expands list elements into another lis
void main() {
var list = [10, 20];
var list2 = [0, 1, ...list];
print(list2); // Output: [0, 1, 10, 20]
}
- Variable Modifiers
final→ Cannot be reassignedconst→ Compile-time constantlate→ Deferred initialization
- Data Types
- Numbers:
int,double - Strings:
String - Boolean:
bool - Objects:
Object - Collections:
List,Set,Map - Enums:
enum - Generics:
Class<Type>
- Numbers:
- Annotations
@override→ Marks an overridden method@deprecated→ Marks deprecated features
Control Structures
Dart supports standard control structures:
Conditional Statements
if (condition) {
// Do something
} else {
// Do something else
}
Switch Statement
switch(value) {
case 1:
// Do something
break;
case 2:
// Do something else
break;
default:
// Default case
}
Loops
for (var i = 0; i < 10; i++) {
print(i);
}
while (condition) {
// Loop while condition is true
}
do {
// Execute at least once
} while (condition);
Exception Handling
try {
// Code that may throw an error
} catch (e) {
print('Error: $e');
} finally {
print('Always executed');
}
Functions & Methods
Optional & Named Parameters
// Optional parameter with default value
void func1(String param1, [int param2 = 0]) {
print("$param1, $param2");
}
// Named parameters (nullable)
void func2({String? param1, int? param2}) {
print("$param1, $param2");
}
// Required named parameters
void func3({required String param1, required int param2}) {
print("$param1, $param2");
}
void main() {
func1("func1"); // Output: func1, 0
func2(param1: "func2"); // Output: func2, null
func3(param1: "func3", param2: 3); // Output: func3, 3
}
First-Class Functions
Functions can be passed as arguments:
void hello() => print('Hello');
void func(Function f) => f();
void main() {
func(hello); // Output: Hello
}
Anonymous Functions
void main() {
Function hello = (name) {
return "Hello, $name";
};
print(hello("Dart")); // Output: Hello, Dart
}
Arrow Function Syntax (=>)
void func(String param1, int param2) => print('$param1 / $param2');
void main() {
func("data", 20); // Output: data / 20
}
Object-Oriented Programming in Dart
Classes & Objects
class Person {
String name;
// Constructor
Person(this.name);
void greet() {
print('Hello, my name is $name');
}
}
void main() {
var person = Person("John");
person.greet(); // Output: Hello, my name is John
}
Inheritance (extends)
class Animal {
void makeSound() {
print("Some sound...");
}
}
class Dog extends Animal {
@override
void makeSound() {
print("Woof!");
}
}
void main() {
var dog = Dog();
dog.makeSound(); // Output: Woof!
}
Abstract Classes
abstract class Shape {
void draw();
}
class Circle extends Shape {
@override
void draw() {
print("Drawing a circle");
}
}
Mixins (with)
mixin Flyable {
void fly() {
print("I can fly!");
}
}
class Bird with Flyable {}
void main() {
var bird = Bird();
bird.fly(); // Output: I can fly!
}
Fields/Methods
- Private Fields/Methods:
_xxx- If a name starts with
_, its scope becomes private.
- If a name starts with
- Static Fields/Methods:
static- Fields and methods that belong to the class itself rather than an instance.
- Setter/Getter:
get/set- Used to access fields in a controlled manner.
// Class Definition
class Hoge {
int _data = 0;
int get data => _data; // Getter
set data(int data) {
this._data = data; // Setter
}
}
// Using the Defined Class
void main() {
Hoge hoge = Hoge();
hoge.data = 10;
print(hoge.data);
}
// Output:
// 10
Other Features
Package Management
Dart supports importing external packages:
import 'package:flutter/material.dart';
Asynchronous Programming
Future<String> fetchData() async {
return "Data loaded";
}
void main() async {
print(await fetchData()); // Output: Data loaded
}
Conclusion
Today, I introduced Dart and its key features!
Next time, I’ll cover setting up the Dart development environment.
Enjoy your Flutter journey!!
2025-03-08
0件のコメント
コメントはまだありません。最初の一人になりましょう!