flutter
/

Dart Programming Language Introduction for Beginners

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

flutter

Dart Programming Language Introduction for Beginners

What is Dart?

Dart is a client-optimized programming language developed by Google. It is designed for building fast, scalable applications across multiple platforms, including mobile, web, desktop, and server. Dart is the primary language used for Flutter, Google's UI toolkit for building natively compiled applications.

Why Learn Dart?

Dart is easy to learn, especially if you have experience with languages like Java, JavaScript, or C#. It offers features like strong typing, null safety, async/await support, and a rich set of core libraries. With Dart, you can build high-performance apps using Flutter, write server-side code, and even create command-line tools.

Your First Dart Program

DARTRead-only
1
void main() {
  print('Hello, Dart!');
}

Every Dart program starts with the main() function. The print() function outputs text to the console.

Variables and Data Types

Dart is a statically typed language but also supports type inference via var. Common data types include:

    • int – integer numbers
    • double – floating-point numbers
    • String – text
    • bool – boolean values (true/false)
    • List – ordered collection of objects
    • Map – key-value pairs
DARTRead-only
1
void main() {
  var name = 'Alice';          // type inferred as String
  int age = 30;                 // explicit int
  double height = 5.8;           // double
  bool isStudent = false;        // boolean
  List<int> scores = [90, 85, 88]; // list of integers
  Map<String, dynamic> person = { // map with dynamic values
    'name': 'Bob',
    'age': 25
  };
  
  print('Name: $name, Age: $age');
}

Control Flow

Dart supports standard control flow statements:

DARTRead-only
1
void main() {
  int number = 10;
  
  // if-else
  if (number > 0) {
    print('Positive');
  } else {
    print('Non-positive');
  }
  
  // for loop
  for (int i = 0; i < 5; i++) {
    print('i = $i');
  }
  
  // while loop
  int count = 0;
  while (count < 3) {
    print('count $count');
    count++;
  }
}

Functions

Functions are first-class citizens in Dart. You can define functions with or without return types, and even pass functions as parameters.

DARTRead-only
1
int add(int a, int b) {
  return a + b;
}

void main() {
  var result = add(5, 3);
  print('Sum: $result'); // Sum: 8
  
  // anonymous function
  var multiply = (int a, int b) => a * b;
  print(multiply(4, 2)); // 8
}

Null Safety

Dart supports sound null safety, meaning variables cannot contain null unless you explicitly allow it. This prevents null reference errors. Use ? after a type to make it nullable, and use ! to assert non-null.

DARTRead-only
1
void main() {
  String? name; // nullable
  name = null;  // allowed
  
  String greeting = 'Hello';
  // String? nullableGreeting = greeting; // OK
  
  // Using null-aware operators
  print(name?.length); // prints null
  
  // If you're sure it's not null, use !
  // print(name!.length); // would throw if name is null
}

Classes and Objects

Dart is an object-oriented language. Classes define blueprints for objects.

DARTRead-only
1
class Person {
  String name;
  int age;
  
  Person(this.name, this.age); // constructor
  
  void sayHello() {
    print('Hello, my name is $name');
  }
}

void main() {
  var person = Person('Alice', 30);
  person.sayHello();
}

Async Programming

Dart provides Future and async/await for asynchronous programming, making it easy to handle tasks like network requests or file I/O.

DARTRead-only
1
Future<String> fetchData() async {
  await Future.delayed(Duration(seconds: 2));
  return 'Data loaded';
}

void main() async {
  print('Loading...');
  var data = await fetchData();
  print(data); // prints after 2 seconds
}

Key Takeaways

    • Dart is a modern, client‑optimized language.
    • It supports both JIT and AOT compilation.
    • Strong typing with type inference and null safety.
    • Object‑oriented with classes and mixins.
    • Excellent for Flutter development.

Try it yourself

void main() {
  print('Welcome to Dart!');
}

Test Your Knowledge

Q1
of 3

What is the entry point of every Dart program?

A
init()
B
start()
C
main()
D
run()
Q2
of 3

Which keyword is used to declare a variable with type inference?

A
let
B
var
C
auto
D
dynamic
Q3
of 3

How do you make a variable nullable in Dart?

A
Add ? after the type
B
Use nullable keyword
C
Use * after the type
D
Variables are nullable by default

Frequently Asked Questions

What is Dart used for?

Dart is a general‑purpose programming language used to build mobile, desktop, web, and server applications. It is best known as the language behind the Flutter UI framework for building natively compiled cross‑platform apps.

Is Dart easy to learn for beginners?

Yes, Dart has a clean syntax similar to languages like Java, JavaScript, and C#. Its strong typing and null safety help catch errors early, making it a great choice for beginners.

Do I need to learn Dart before Flutter?

Yes, Flutter uses Dart, so understanding Dart fundamentals (variables, functions, classes, async) will make learning Flutter much easier. However, you can learn Dart alongside Flutter.

What platforms does Dart support?

Dart can be compiled to native code for mobile (iOS, Android) and desktop (Windows, macOS, Linux), as well as to JavaScript for the web. This makes it a true multi‑platform language.

What is null safety in Dart?

Null safety is a feature that prevents variables from containing null unless explicitly allowed. It eliminates a whole class of null reference errors and makes code more robust. Introduced in Dart 2.12.

Is Dart only for Flutter?

No, Dart can be used for a variety of applications beyond Flutter: command‑line tools, server‑side applications (with frameworks like Aqueduct or Angel), and even full‑stack web development.

How does Dart compare to JavaScript?

Dart is statically typed and compiled, offering better performance and tooling for large‑scale apps. It has features like isolates for concurrency and optional sound null safety, whereas JavaScript is dynamically typed and runs in browsers.

What tools do I need to start coding in Dart?

You need the Dart SDK, which includes the compiler, VM, and package manager (pub). You can write code in any text editor, but using an IDE like VS Code or IntelliJ with Dart plugins is recommended.

Next

dart installation

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.