-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.md.backup
More file actions
423 lines (320 loc) · 12.3 KB
/
README.md.backup
File metadata and controls
423 lines (320 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<img src="./img_resources/dart_log.png" height="10%" width ="10%">
# Dart Programming Fast and Simple Guide
*This Guide is for those, who were/are already familiar to programming and have desire to learn dart also for various purposes.*

---
**Links :**
<a href="https://ninzarj01.github.io/DartProgrammingLanguageGuide/" style="text-align: center;">
<img src="./img_resources/docsify_logo.png" height="4%" width="15%">
Docsified WebPage Of Guide</a>
---
**Note :** This guide not gonna brush over the simple programming facts like
- what is loop ?
- what is datatype ?
- what is condition statement ?
- what is variable ? and so on.
If you are unfamiliar with any of above terms take a slight look on this terms before starting.
---
First of all we gonna cover basic syntax and topics on of dart language like :
1. Data Types
1. How to Define A Variable
2. Print Statement in Dart
3. Different operators in Dart
4. Loops in Dart
5. If Else Statement in Dart
6. Function in Dart
**Before We go onto basics, let us see an hello world program in dart language**
```dart
main(){
print("Hello World");
}
```
## 1.Different Primitive DataTypes in Dart :
**Before Starting :** All datatypes in dart are at base is just an object.
Value of each variable declared using these datatype is **null** by default.
---
**More Remarks** :
1. <ins>Dart is statically typed language</ins> means you have to explicitly declare variable's datatype and it cannot be changed at runtime.
1. **var** keyword is used to auto declare the datatype of variable but dissimilar of _var in js_
once variable datatype set using var keyword it can't be changed.
Note : by default var assign the dynamic datatype to variable but it can be differ if you initialize the variable during declaration.
```dart
var var1 ;//dynamic
var1 = 'a';
var1 = 2;//No Errors
var var2 = 123;//int
var2 = "fire";//Error: A value of type 'String' can't be assigned to a variable of type 'int'.
```
1. **dynamic** keyword which is dart object under the hood is used to dynamically assign datatype to variable it associated with.
```dart
dynamic a = 40;
a ='a'; //works
a = true;
a = ['a','b',2,true];
```
---
- ### **Numbers**
holds the numeric value.
The num type is an inherited data type of the int and double types.
| **Keyword** - num |
|Number in dart can further classified as:|
|------------------|
| **int** used to represent integers|
| **double** used to represent floating point values|
---
- #### *int*
is used to represent whole numbers values.
| **Keyword** - int |
```dart
int mynum = 4;
int mynum2 = 9999;
int mynum3 = -232;
//below code will give an error
int mynum4 = 33.34;//Error: A value of type 'double' can't be assigned to a variable of type 'int'.
//Note : Sometime you may need to convert int to string datatype at this you may use toString method in-built to num object.
print("1 "+ mynum.toString());
print("2 "+ mynum2.toString());
print("3 "+ mynum3.toString());
/*Output :
1 4
2 9999
3 -232
*/
```
- #### *double*
is used to represent 64-bit floating-point numbers.
| **Keyword** - double |
```dart
double mynum = 434.22;
double mynum1 = -4331.233;
double mynum2 = 32;
//Note : Sometime you may need to convert double to string datatype at this you may use toString method in-built to num object.
print("1 "+ mynum.toString());
print("2 "+ mynum1.toString());
print("3 "+ mynum2.toString());
/*Output:
1 434.22
2 -4331.233
3 32
*/
```
>> <a href="https://github.com/NinzaRJ01/DartProgrammingLanguageGuide/tree/master/MajorDataTypesInDart/Num_Class">For more detail on Number, Int, Double class and their property</a>
- ### **Strings**
is used to represent a sequence of characters. It is a sequence of UTF-16 code units. The keyword string is used to represent string literals.
| **Keyword** - String |
- In dart string is represent as text between ""(double quotes) or ''(single quotes ) also known as *String Literals*.
```dart
String mystr = "hello";
String mystr2 = "dart";
String mystr3 = "652";
String mystr4 = 54;
//Error: A value of type 'int' can't be assigned to a variable of type 'String'
//String mystr4 = 54;
print("1 "+mystr);
print("2 "+mystr2);
print("3 "+mystr3);
/*Output
1 hello
2 dart
3 652
*/
```
>> <a href="./MajorDataTypesInDart/String_Class">For more detail on String class in dart.</a>
- ### **Booleans**
represents Boolean values true and false.
<ins>The keyword **bool** is used to represent a Boolean literal in DART. </ins>
| **Keyword** - bool |
```dart
bool mybool = false;
bool mybool1 = true;
bool mybool2 = 1;//Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
bool mybool3 = 0;//Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
bool mybool4;
bool mybool5 ="true";//Error: A value of type 'String' can't be assigned to a variable of type 'bool'.
bool mybool6 ="false";//Error: A value of type 'String' can't be assigned to a variable of type 'bool'.
print("1 "+mybool.toString());
print("2 "+mybool1.toString());
print("3 "+mybool4.toString());//null is no value is given
```
- ### **Lists (similar to Arrays)**
is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects.
| **Keyword** - List |
*Remarks* : . The <ins>dart:core library</ins> provides the List class that enables creation and manipulation of lists.
- #### **Fixed Sized List**
In this type of list, length can't change at runtime.
- **Declaration Syntax :**
>> List Declare using this syntax will be fixed size list.
```dart
List list-name = new List(initialization_size);
```
>> Intializing list element after declaration
```dart
List myList = new List(n);
myList[index] = val;//index can be any value between 0 to n-1
```
- **Example of Nested Lists :-**
```dart
List myList = new List(6);
for(int i=0;i<6;i++){
myList[i]= "hello "+i.toString();
}
List myList2 = ["one",2,"three"];
List myList3 =[myList,myList2];
print(myList);
print(myList2);
print(myList3);
/* Output :
[hello 0, hello 1, hello 2, hello 3, hello 4, hello 5]
[one, 2, three]
[[hello 0, hello 1, hello 2, hello 3, hello 4, hello 5], [one, 2, three]]
*/
```
- #### **Resizable List**
->Syntax is same as of Fixed List but you can add more element, change the length of current List object via **add()** method.
Note : A list is resizable only if initialized using following syntax;
- Syntax1 :
```dart
List l = new List();//empty initialization of list using constructor with no para
//l is a variable-name
```
- Syntax2 :
```dart
List l = [];
//Using List literal or
List l = [val1,val2,...val_n];
//l is a variable-name
```
- First method:
```dart
List myList = new List();
myList.add(1);
myList.add(2);
myList.add(3);
myList.add("hy");
myList.add("true");
myList.add(true);
//i won't recommend this if input value need to be changed after initialization.
```
- Second method:
```dart
List myList2 = ['a','b','c'];//resize the existing list
myList2.add(4);
print(myList);
print(myList2);
/*Output
[1, 2, 3, hy, true, true]
[a, b, c, 4]
*/
myList2[4] ="five";//This generate out of index error.
```
- More Examples :
```dart
List myList = new List(6);
for(int i=0;i<6;i++){
myList[i]= "hello "+i.toString();
}
List myList2 = ["one",2,"three"];
myList2.add(3);
List myList3 =[myList,myList2];
var myList4 = new List();
myList4.add(5);
// myList3[2]=5;
print(myList);
print(myList2);
print(myList3);
print(myList4);
/*Output
[hello 0, hello 1, hello 2, hello 3, hello 4, hello 5]
[one, 2, three, 3]
[[hello 0, hello 1, hello 2, hello 3, hello 4, hello 5], [one, 2, three, 3]]
[5]
*/
```
- #### **Fixed Size List vs Resizable List**
```dart
List l = new List(2);//Fixed Size List
l[0]=2;
l.add(1);//It will Throw
//Unsupported operation: addError: Unsupported operation: add
print(l);
List p = new List();
p.add(2);//No problem
```
>> <a href=" ">For More Detail about List class in dart</a>
- ### **Maps**
object is a key and value pair. Keys and values on a map may be of any type. It is a dynamic collection.
| **Keyword** - Map |
- **Syntax :**
```dart
Map myMap = new Map();
myMap[key1] =val1;
myMap[key2] =val2;
myMap[key3] =val3;
....
myMap[key_n] =val_n;
//val1,val2,val3...val_n can be of any datatypes, objects etc.
//key1,key2,key3...key_n can be of any datatypes,objects etc.
```
- Example :
```dart
Map obj = new Map();
obj["mykey"] ="myVal";//String as key
List l = [1,2,3];//list
obj[l] ="list as key";//list as key
obj[true] =false;//boolean value as key
obj[23] =43;//integer value as key
print(obj["mykey"]);
print(obj[l]);
print(obj[true]);
print(obj[23]);
/*Output :
myVal
list as key
*/
```
>>> **Note** while using list or any other object as key that it will take the _reference of obj_ as key not the value of object itself.
- Example :
```dart
Map myMap = new Map();
List l = [1,2,3];//list
obj[l] ="List as key";
print("Before : "+ obj[l]);
l[1] = 4;//changes
print("After : "+obj[l]);//same result
/*Output
Before : List as key
After : List as key
*/
//More
obj[[5,6,7]]= "Another One";
print(obj[[5,6,7]]);
//and also
print(obj[[1,2,3]]);
//since l is reference to object of List which is new for each list
print([1,2,3]==l);//Example
```
>><a href=" ">For More Detail about Map class in dart</a>
><h3 align="center">More About Dart</h3>
- ### **Is dart an Interpreted or a Compiled Language ?**
Actually, it can be both interpreted and compiled. Yes, it provides functionality for _ahead of time_ (AOT) compilation used primarily in **production code** and also for _just in time_ compilation which is beneficial while **developing code**.
Commands for compiling different dart executables :
```bash
dart compile <sub-command> <dart-file>
```
#### **Sub-Commands :**
|sub-commands | overview |
|-------------|----------|
|aot-snapshot | Compile Dart to an AOT snapshot.|
|exe | Compile Dart to a self-contained executable.|
|jit-snapshot | Compile Dart to a JIT snapshot.|
|js | Compile Dart to JavaScript.|
|kernel |Compile Dart to a kernel snapshot|
**Example (for binary system executable) :**
- Creating
```bash
dart compile exe main.dart
```
- Running (in linux env)
```bash
./main.exe
```