|
| 1 | +--- |
| 2 | +id: flutter-dart-introduction |
| 3 | +title: Flutter - Introduction to Dart Programming |
| 4 | +sidebar_label: Flutter - Introduction to Dart Programming |
| 5 | +sidebar_position: 4 |
| 6 | +tags: [introduction, Flutter, App development] |
| 7 | +description: Introduction to Dart Programming |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +Dart is an open-source general-purpose programming language developed by Google. It is an object-oriented language with C-style syntax. Dart supports programming concepts like interfaces and classes. However, unlike other programming languages, Dart doesn't support arrays. Instead, Dart collections can be used to replicate data structures such as arrays, generics, and optional typing. |
| 12 | + |
| 13 | +Here is a simple Dart program: |
| 14 | + |
| 15 | +```dart |
| 16 | +void main() { |
| 17 | + print("Dart language is easy to learn"); |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## Variables and Data Types |
| 22 | + |
| 23 | +In Dart, variables are named storage locations, and data types refer to the type and size of data associated with variables and functions. |
| 24 | + |
| 25 | +Dart uses the `var` keyword to declare variables. For example: |
| 26 | + |
| 27 | +```dart |
| 28 | +var name = 'Dart'; |
| 29 | +``` |
| 30 | + |
| 31 | +The `final` and `const` keywords are used to declare constants. For example: |
| 32 | + |
| 33 | +```dart |
| 34 | +void main() { |
| 35 | + final a = 12; |
| 36 | + const pi = 3.14; |
| 37 | + print(a); |
| 38 | + print(pi); |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Dart language supports the following data types: |
| 43 | + |
| 44 | +- Numbers: Used to represent numeric literals, such as integers and doubles. |
| 45 | +- Strings: Represents a sequence of characters. String values are specified in either single or double quotes. |
| 46 | +- Booleans: Dart uses the `bool` keyword to represent Boolean values, `true` and `false`. |
| 47 | +- Lists and Maps: Used to represent a collection of objects. For example: |
| 48 | + |
| 49 | +```dart |
| 50 | +void main() { |
| 51 | + var list = [1, 2, 3, 4, 5]; |
| 52 | + print(list); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +The above code produces the output: `[1, 2, 3, 4, 5]`. |
| 57 | + |
| 58 | +Map can be defined as shown here: |
| 59 | + |
| 60 | +```dart |
| 61 | +void main() { |
| 62 | + var mapping = {'id': 1, 'name': 'Dart'}; |
| 63 | + print(mapping); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The above code produces the output: `{'id': 1, 'name': 'Dart'}`. |
| 68 | + |
| 69 | +Dynamic: If the variable type is not defined, then its default type is dynamic. For example: |
| 70 | + |
| 71 | +```dart |
| 72 | +void main() { |
| 73 | + dynamic name = "Dart"; |
| 74 | + print(name); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## Decision Making and Loops |
| 79 | + |
| 80 | +Dart supports decision-making statements like `if`, `if..else`, and `switch`. It also supports loops like `for`, `for..in`, `while`, and `do..while`. Here's an example: |
| 81 | + |
| 82 | +```dart |
| 83 | +void main() { |
| 84 | + for (var i = 1; i <= 10; i++) { |
| 85 | + if (i % 2 == 0) { |
| 86 | + print(i); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +The above code prints the even numbers from 1 to 10. |
| 93 | + |
| 94 | +## Functions |
| 95 | + |
| 96 | +A function is a group of statements that together performs a specific task. Here's an example of a simple function in Dart: |
| 97 | + |
| 98 | +```dart |
| 99 | +void main() { |
| 100 | + add(3, 4); |
| 101 | +} |
| 102 | +
|
| 103 | +void add(int a, int b) { |
| 104 | + int c; |
| 105 | + c = a + b; |
| 106 | + print(c); |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +The above function adds two values and produces the output: `7`. |
| 111 | + |
| 112 | +## Object-Oriented Programming |
| 113 | + |
| 114 | +Dart is an object-oriented language that supports features like classes and interfaces. A class is a blueprint for creating objects and includes fields, getters and setters, constructors, and functions. Here's an example: |
| 115 | + |
| 116 | +```dart |
| 117 | +class Employee { |
| 118 | + String name; |
| 119 | + |
| 120 | + // Getter method |
| 121 | + String get emp_name { |
| 122 | + return name; |
| 123 | + } |
| 124 | + |
| 125 | + // Setter method |
| 126 | + void set emp_name(String name) { |
| 127 | + this.name = name; |
| 128 | + } |
| 129 | + |
| 130 | + // Function definition |
| 131 | + void result() { |
| 132 | + print(name); |
| 133 | + } |
| 134 | +} |
| 135 | +
|
| 136 | +void main() { |
| 137 | + // Object creation |
| 138 | + Employee emp = new Employee(); |
| 139 | + emp.name = "employee1"; |
| 140 | + emp.result(); // Function call |
| 141 | +} |
| 142 | +``` |
0 commit comments