Dart Tu To
Dart Tu To
Dart Tu To
final: A variable marked as final can be set only once. After being initialized,
its value cannot be changed.
-------------------------------
declaring a list :
void main()
{
var 1st = new List(3);
1st[0] = 12;
2st[1] = 13;
1st[2] = 11;
print(1st);
Growable List :
void main()
{
var num_list = [1,2,3];
print(num_list);
//[1,2,3]
}
void main()
{
var 1st = new List();
1st.add(12);
1st.add(13);
print(1st);
}
//[12,13]
updating a list :
void main() {
List l = [1,2,3,4,5,6,7,8,9];
print('The value of list before replacing ${l}');
l.replaceRange(0,3,[11,23,24]);
print('The value of list after replacing the items between the range [0-3] is $
{l}');
}
-----------------
removing list items :
void main()
{
List l = [1,2,3,4,5,6,7,8,9];
print('The value of list before removing the list element ${l}');
bool res = l.remove(1);
print('The value of list after removing the list element ${l}');
}
----------------------------------------------
List.removeAt()
The List.removeAt function removes the value at the specified index and returns it.
List.removeAt(int index)
___________________________________________________________________________________
__________________________
Map:
To declare a map using map literals, you need to enclose the key-value pairs within
a pair of curly brackets "{ }".
exempla :
void main() {
var details = {'Usrname':'tom','Password':'pass@123'};
print(details);
}
2. declaring a Map suing a Map Constructor:
step1:
var identifier = new Map()
step2:
map_name[key] = value
----------
void main() {
var details = new Map();
details['Usrname'] = 'admin';
details['Password'] = 'admin@123';
print(details);
}
--------------------------------------------
String.codeUnitAt() Function
Code units in a string can be accessed through their indexes. Returns the 16-bit
UTF-16 code unit at the given index.
String.codeUnitAt(int index);
-----------------------------------------------------------------------------------
---------------------------
The enum_name specifies the enumeration type name:
void main() {
Weather currentWeather = Weather.sunny;
switch (currentWeather) {
case Weather.sunny:
print('It is sunny today! Wear sunglasses .');
break;
case Weather.cloudy:
print('It is cloudy today. Might rain later . ');
break;
case Weather.rainy:
print('It is raining, Do not forget your umbrella.');
break;
case Weather.snowy:
print('It is snowy , Dress warmly .');
break;
case Weather.windy:
print('it is windy , hold onto your hat!');
break;
}
}
-------------------------------------------------
functions :
'void' function_name() {
//statements
}
optional Parameters :
>>>Optional Positional Parameter::
void main() {
test_param(123);
}
test_param(n1,[s1]) {
print(n1);
print(s1);
}
-------------------------------------------
function_name(optional_param:value,…);
exemple :
void main() {
test_param('hey');
test_param(2000,s1:'yepIdidIt');
test_param('niCeee',s2:'hello',s1:'world');
}
test_param(n1,{s1,s2}) {
print(n1);
print(s1);
print('-------');
}
-------------------------
hey
null
-------
2000
yepIdidIt
-------
niCeee
world
-------
-------------------------------
Lambda Functions :
[return_type]function_name(parameters)=>expression;
void main()
{
printMsg();
print(test());
}
printMsg()=>
print("hello");
int test()=>123;
// returning function
------------------------------------------------
interface :
void main() {
ConsolePrinter cp= new ConsolePrinter();
cp.print_data();
}
class Printer {
void print_data() {
print("__________Printing Data__________");
}
}
class ConsolePrinter implements Printer {
void print_data() {
print("__________Printing to Console__________");
}
}
-----------------------------------------
classes :
void main() {
Car c1 = new Car.namedConst('E1001');
Car c2 = new Car();
}
class Car {
Car() {
print("Non-parameterized constructor invoked");
}
Car.namedConst(String engine) {
print("The engine is : ${engine}");
}
}
Dart, unlike other programming languages, doesn’t support arrays. Dart collections
can be used to replicate data structures like an array
List:
void main() {
List logTypes = new List();
logTypes.add("WARNING");
logTypes.add("ERROR");
logTypes.add("INFO");
---------------------
Set:
syntax:
void main() {
Set numberSet = new Set();
numberSet.add(100);
numberSet.add(20);
numberSet.add(5);
numberSet.add(60);
numberSet.add(70);
print("Default implementation :${numberSet.runtimeType}");
// all elements are retrieved in the order in which they are inserted
for(var no in numberSet) {
print(no);
}
}
100
20
5
60
70
----------------------------------
Generics:
void main() {
List <String> logTypes = new List <String>();
logTypes.add(1);
logTypes.add("ERROR");
logTypes.add("INFO");
------------------------------
void main() {
Set <int>numberSet = new Set<int>();
numberSet.add(100);
numberSet.add(20);
numberSet.add(5);
numberSet.add(60);
numberSet.add(70);
// numberSet.add("Tom");
compilation error;
print("Default implementation :${numberSet.runtimeType}");
for(var no in numberSet) {
print(no);
}
}
--------------------------------------------------
import 'dart:collection';
void main()
{
Queue<int> queue = new Queue<int>();
print("Default implementatio, ${queue,runtimeType}");
queue.addLast(10);
queue.addLast(30);
queue.addLast(40);
queue.removeFirst();
for(int no in queue){
print(no);
}
}
_______________________________________________________________________
Every now and then, developers commit mistakes while coding. A mistake in a program
is referred to as a bug. The process of finding and fixing bugs is called debugging
and is a normal part of the development process. This section covers tools and
techniques that can help you with debugging tasks.
-----------------------------------------------------
-------------------------------------------------------
Libraries :
import 'URI'
dart:io ->>> File, socket, HTTP, and other I/O support for server applications.
This library does not work in browser-based applications. This library is imported
by default.
dart:core ->>>
Built-in types, collections, and other core functionality for every Dart program.
This library is automatically imported
----------------------------------------------------------
library calculator_lib;
import 'dart:math';
void main() {
var num1 = 10;
var num2 = 20;
var sum = add(num1,num2);
var mod = modulus(num1,num2);
var r = random(10);