Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Dart Tu To

Télécharger au format txt, pdf ou txt
Télécharger au format txt, pdf ou txt
Vous êtes sur la page 1sur 10

les variables :

* the dynamic keyword:

Variables declared without a static type are implicity declared as dynamic ,


Variables can be also declared using the dynamic keyword in place of the var
keyword .

* Final and Const :

final: A variable marked as final can be set only once. After being initialized,
its value cannot be changed.

const : A variable marked as const is a compile-time constant. Once assigned, its


value cannot be altered, and it must be known at compile-time.

-------------------------------

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}');
}

//The value of list before replacing [1, 2, 3, 4, 5, 6, 7, 8, 9]


The value of list after replacing the items between the range [0-3] is [11, 23, 24,
4, 5, 6, 7, 8, 9]

-----------------
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}');
}

/*The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]


The value of list after removing the list element [2, 3, 4, 5, 6, 7, 8, 9] */

----------------------------------------------
List.removeAt()
The List.removeAt function removes the value at the specified index and returns it.

List.removeAt(int index)

___________________________________________________________________________________
__________________________

Map:

Maps can be declared in two ways −

Using Map Literals


Using a Map constructor

1.Declaring a Map using Map Literals:

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);
}

--------------------------------------------

Strings are a sequence of characters. Dart represents strings as a sequence of


Unicode UTF-16 code units. Unicode is a format that defines a unique numeric value
for each letter, digit, and symbol.

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:

The enumeration list is a comma-separated list of identifiers :

enum Weather { sunny, cloudy, rainy, snowy, windy }

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);
}

-------------------------------------------

Optional Named Parameter:

void function_name(a, {optional_param1, optional_param2}) { }

Syntax - Calling the function:

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}");
}
}

The engine is : E1001


Non-parameterized constructor invoked
___________________________________________________

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");

// iterating across list


for(String type in logTypes){
print(type);
}

// printing size of the list


print(logTypes.length);
logTypes.remove("WARNING");

print("size after removing.");


print(logTypes.length);
}

---------------------
Set:

syntax:

Identifier = new Set()

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");

//iterating across list


for (String type in logTypes) {
print(type);
}
}

------------------------------

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.
-----------------------------------------------------

A typedef, or a function-type alias, helps to define pointers to executable code


within memory. Simply put, a typedef can be used as a pointer that references a
function.

Step 1: Defining a typedef:


typedef function_name(parameters)

Step 2: Assigning a Function to a typedef Variable


type_def var_name = function_name

-------------------------------------------------------

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

dart: math ->>>


Mathematical constants and functions, plus a random number generator.

dart: convert ->>>


Encoders and decoders for converting between different data representations,
including JSON and UTF-8.

----------------------------------------------------------

library calculator_lib;
import 'dart:math';

int add(int firstN,int secN)


{
print("inside add method of Calculator library");
return firstN+secN;
}
int modulus(int firstNumber,int secondNumber)
{
print("inside modulus method of Calculator Library ");
return firstNumber % secondNumber;
}
int random(int no)
{
return new Random().nextInt(no);
}

void main() {
var num1 = 10;
var num2 = 20;
var sum = add(num1,num2);
var mod = modulus(num1,num2);
var r = random(10);

print("$num1 + $num2 = $sum");


print("$num1 % $num2= $mod");
print("random no $r");
}

Vous aimerez peut-être aussi