As promised in my last blog on Why you should learn Flutter today, we'll be talking about Dart, a versatile and powerful programming language that's gaining popularity for building web, mobile, and desktop applications. In just under 10 minutes, you'll get a practicable grasp of Dart's syntax.
What is Dart?
Dart is a type-safe general-purpose programming language known for its simplicity, speed, and flexibility, designed for making fast apps on multiple platforms, developed by Lars Bak and Kasper Lund at Google. Today It is one of the most loved programming languages in the Industry, let's find out why!
Dart Basics: Syntax and Structure
Dart's syntax is user-friendly and resembles several other programming languages you might be familiar with, like Java, JavaScript, and C#.
Variables and Data Types: Declare variables using
var
or explicitly define types likeint
,double
,String
, andbool
. Just like JavaScript, Dart usesvar
for type inference.Data Structures: Dart provides various built-in data structures that you can use to manage and manipulate data efficiently. Some of them are given below
List<int> numbers = [1, 2, 3, 4, 5,];
List<String> names = ['CC', 'Visit', 'Medusa',];
// "," is allowed after last element and actually recommended for better syntax
Set<int> uniqueNumbers = {1, 2, 3, 4, 5,};
Set<String> uniqueNames = {'Medusa', 'Visit', 'Deolll',};
Map<String, int> ages = {'Joel': 56, 'Ellie': 19,};
- Functions: Define functions using the
<functionName>(parameters) => expression;
similar to JavaScript's arrow functions, anonymous(Lambda) functions are also supported.
int add(int a, int b) {
return a + b;
}
void main() {
int sum = add(5, 3);
}
- Control Flow: Use
if
,else if
, andelse
for conditional statements, and usefor-in
,for-each
while
anddo-while
loops for iteration. The syntax will feel comfortable if you've used languages like C++ or Java.
//For Loop
for (int i = 1; i <= 5; i++) {
print('Number $i');
}
//For-in Loop
for (int number in numbers) {
print('Number: $number');
}
//While Loop
int count = 0;
while (count < 5) {
print('Count: $count');
count++;
}
//Do-While Loop
int num = 0;
do {
print('Num: $num');
num++;
} while (num < 5);
- Classes and Objects: Create classes using
class ClassName { ... }
and instantiate objects with constructors. If you've worked with object-oriented languages, this will look familiar.
class Person {
String name; // Public field
int _age; // Private field "_", at the start makes private
Person(this.name, this._age);
// Public method
void introduceYourself() {
print('Hello, my name is $name.');
_privateMethod(); // Accessing private method within the class
}
// Private method
void _privateMethod() {
print('This is a private method.');
}
}
NOTE: Dart does not have a true protected access modifier, it relies on a naming convention using an underscore _
to indicate that a member is intended for internal use within a package.
- Null Safety: Dart embraces null safety, helping you avoid null reference errors. Use
?
for nullable variables and!
to assert non-null.
void main() {
String name = 'CC';
// name = null; // name cannot be null now
String? nullableName = 'Keh'; // Notice the declaration using "?" after dtype
nullableName = null; // nullableName can be null now
String? anothernullableName = 'Deolll';
// Using the "!" operator to assert that nullableName is non-null
String nonNullableName = anothernullableName!;
}
Easy Right!?
Although the language is quite easy but remember, practice is key! Try out these concepts in your favorite Dart environment try Dart Pad and watch your proficiency grow, for more information visit dart. dev for their documentation.
Happy coding! ๐ฅ๐ฅ๐ฏ๐ฏ