Dart Syntax: Basic and Advanced

Basic Dart Syntax

Let's start with fundamental Dart syntax:

1. Variables

var variableName = value; dynamic dynamicVariable = 'Dart is dynamic';

2. Data Types

int age = 25; double piValue = 3.14; String name = 'Dart'; bool isFlutterAwesome = true;

3. Functions

int add(int a, int b) { return a + b; } // Shorter syntax int multiply(int a, int b) => a * b;

4. Control Flow

if (condition) { // code } else { // code } for (var i = 0; i < 5; i++) { // code } while (condition) { // code }

5. Collections

List numbers = [1, 2, 3]; Set uniqueNames = {'John', 'Jane'}; Map person = {'name': 'Alice', 'age': 30};

Advanced Dart Syntax

Now, let's explore some advanced Dart concepts:

1. Asynchronous Programming

Future fetchData() async { // asynchronous code }

2. Classes and Objects

class Person { String name; int age; Person(this.name, this.age); } var person = Person('John', 25);

3. Generics

List numbers = [1, 2, 3]; Map data = {'key': 'value'};

4. Exception Handling

try { // code that might throw an exception } catch (e) { // handle the exception }

5. Flutter Widgets (Flutter-Specific)

import 'package:flutter/material.dart'; class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('My Flutter App')), body: Center(child: Text('Hello, Flutter!')), ), ); } }

This is a brief overview; Dart is a versatile language used in various contexts, including web and mobile development with Flutter. Refer to the official documentation for more in-depth information.

0 Comment:

Post a Comment