Dart is a client-optimized programming language for fast apps on any platform. It is developed by Google and is used to build mobile, desktop, server, and web applications. Dart is the language behind Flutter, which is a popular framework for building natively compiled applications for mobile, web, and desktop from a single codebase.
Key Features of Dart
- Optimized for UI: Dart is designed to create smooth and high-performance user interfaces.
- Productive Development: Dart offers a rich set of libraries and tools that make development faster and more efficient.
- Fast on All Platforms: Dart compiles to ARM and x64 machine code for mobile, desktop, and backend. For web, Dart compiles to JavaScript.
- Strongly Typed: Dart is a statically typed language, which means that type checking is done at compile time.
Setting Up Dart
Before you start coding in Dart, you need to set up your development environment. Here’s how you can do it:
-
Install Dart SDK:
- Visit the Dart SDK page and follow the instructions for your operating system.
- Verify the installation by running
dart --version
in your terminal.
-
Install an IDE:
- You can use any text editor or IDE, but Visual Studio Code and IntelliJ IDEA are popular choices.
- Install the Dart plugin for your chosen IDE to get features like syntax highlighting, code completion, and debugging.
Your First Dart Program
Let's start with a simple "Hello, World!" program in Dart.
Explanation
void main() { ... }
: This is the main function, the entry point of every Dart application.print('Hello, World!');
: This line prints the string 'Hello, World!' to the console.
Dart Syntax Basics
Variables
In Dart, you can declare variables using var
, final
, or const
.
void main() { var name = 'John'; // Variable that can change final age = 30; // Variable that cannot change const pi = 3.14; // Compile-time constant }
Data Types
Dart supports various data types, including:
- Numbers:
int
,double
- Strings:
String
- Booleans:
bool
- Lists:
List
- Maps:
Map
Example
void main() { int age = 25; double height = 5.9; String name = 'Alice'; bool isStudent = true; List<String> colors = ['red', 'green', 'blue']; Map<String, int> scores = {'math': 90, 'science': 85}; print('Name: $name'); print('Age: $age'); print('Height: $height'); print('Is Student: $isStudent'); print('Colors: $colors'); print('Scores: $scores'); }
Control Flow Statements
Dart supports standard control flow statements like if
, else
, for
, while
, and switch
.
Example
void main() { int score = 85; if (score >= 90) { print('Grade: A'); } else if (score >= 80) { print('Grade: B'); } else { print('Grade: C'); } for (int i = 0; i < 5; i++) { print('i: $i'); } int count = 0; while (count < 3) { print('count: $count'); count++; } switch (score) { case 90: print('Excellent'); break; case 85: print('Very Good'); break; default: print('Good'); } }
Practical Exercise
Task
Write a Dart program that calculates the area of a circle. The program should prompt the user to enter the radius and then display the calculated area.
Solution
import 'dart:io'; void main() { print('Enter the radius of the circle:'); String? input = stdin.readLineSync(); double radius = double.parse(input!); double area = 3.14 * radius * radius; print('The area of the circle is: $area'); }
Explanation
import 'dart:io';
: This imports thedart:io
library, which is used for input and output operations.String? input = stdin.readLineSync();
: This reads a line of text from the console.double radius = double.parse(input!);
: This converts the input string to a double.double area = 3.14 * radius * radius;
: This calculates the area of the circle.print('The area of the circle is: $area');
: This prints the calculated area to the console.
Summary
In this section, you learned the basics of Dart, including its key features, how to set up the development environment, and basic syntax. You also wrote your first Dart program and learned about variables, data types, and control flow statements. Finally, you completed a practical exercise to reinforce your understanding. In the next section, we will dive deeper into variables and data types in Dart.
Flutter Development Course
Module 1: Introduction to Flutter
- What is Flutter?
- Setting Up the Development Environment
- Understanding Flutter Architecture
- Creating Your First Flutter App
Module 2: Dart Programming Basics
- Introduction to Dart
- Variables and Data Types
- Control Flow Statements
- Functions and Methods
- Object-Oriented Programming in Dart
Module 3: Flutter Widgets
- Introduction to Widgets
- Stateless vs Stateful Widgets
- Basic Widgets
- Layout Widgets
- Input and Form Widgets
Module 4: State Management
Module 5: Navigation and Routing
Module 6: Networking and APIs
- Fetching Data from the Internet
- Parsing JSON Data
- Handling Network Errors
- Using REST APIs
- GraphQL Integration
Module 7: Persistence and Storage
- Introduction to Persistence
- Shared Preferences
- File Storage
- SQLite Database
- Using Hive for Local Storage
Module 8: Advanced Flutter Concepts
- Animations in Flutter
- Custom Paint and Canvas
- Platform Channels
- Isolates and Concurrency
- Performance Optimization
Module 9: Testing and Debugging
Module 10: Deployment and Maintenance
- Preparing for Release
- Building for iOS
- Building for Android
- Continuous Integration/Continuous Deployment (CI/CD)
- Maintaining and Updating Your App