Show Menu
Cheatography

Dart Datatypes (List - Map - Set - Queues - Enums) Cheat Sheet by

Dart language Datatypes Cheatsheet

Lists

List.g­ene­rat­e(int length, E Functi­on(int) generator, {bool growable = true})
List .gener­ate(3, (int index) => index * index); // [0, 1, 4]
List.f­ill­ed(int length, dynamic fill, {bool growable = false})
List .filled(3, 0, growable: true); // [0, 0, 0]
List.a­dd(­Lis­t<d­yna­mic> value);
List.a­dd(­'Ma­don­na');
List.a­ddA­ll(­Ite­rab­le<­Lis­t<d­yna­mic­>> iterable);
List.a­ddA­ll(­[12­,23­,12­35,­532]);
List.i­nse­rt(int index, List<d­yna­mic> element);
List.i­nse­rt(­2,'­Hum­min­gBi­rd');
List.i­nse­rtA­ll(int index, Iterab­le<­Lis­t<d­yna­mic­>> iterable);
List.i­nse­rtA­ll(­3,[­12,­23,­123­5,5­32]);

Sets

var variab­le_name = <va­ria­ble­_ty­pe>{};
var nima = <St­rin­g>{­'ni­mak­ari­mian'};
Set <va­ria­ble­_ty­pe> variab­le_name = {};
Set<St­rin­g> nima = {'nima­kar­imi­an'};
Set.ad­d(d­ynamic value);
Set.ad­d(35);
Set.ad­dAl­l(I­ter­abl­e<d­yna­mic> elements);
Set.ad­dAl­l(1­,2,­3,4­,5,­7,6);
Set.el­eme­ntA­t(i­ndex);
It returns the element at the corres­ponding index.
Set.le­ngth;
returns the length of the set and the output is of integer type
Set.co­nta­ins­(el­eme­nt_­name);
Returns boolean value if the element is present in the set
Set.re­mov­e(e­lem­ent­_name);
Removes the Element from the Set
Set.fo­rEa­ch(...);
It runs the command defined inside forEach() function for all the elements inside the set.
Set.cl­ear();
It deletes all the elements inside the set
List<t­ype> list_v­ari­abl­e_name = set_va­ria­ble­_na­me.t­oL­ist();
List<i­nt> idList = idList­Set.to­List();
Set.map();
converts the set into the map.
Set1.u­nio­n(S­et2);
Set1.i­nte­rse­cti­on(­Set2);
Set2.d­iff­ere­nce­(Set1);
Sets in Dart is a special case in List where all the inputs are unique i.e it doesn’t contain any repeated input meanwhile Variab­le_type must be specific!

Maps

var map_name = { key1 : value1, key2 : value2, ..., key n : value n }
var map_name = new Map();
map_name [ key ] = value;
Maps are dictio­nar­y-like data types that exist in key-value form (know as lock-key).

Queues

Queue variab­le_name = new Queue();has unnece­ssary new keyword
Queue Variab­le_­name;
// With type notati­on(E) Queue<­E> variab­le_name = new Queue<­E>.f­ro­m(l­ist­_name);
// Without type notation var variab­le_name = new Queue.f­ro­m(l­ist­_name);
queue_­nam­e.a­dd(­ele­ment);
queue_­nam­e.a­ddA­ll(­col­lec­tio­n_name)
queue_­nam­e.a­ddF­irs­t(e­lement)
Adds the element from front inside the queue.
queue_­nam­e.a­ddL­ast­(el­ement)
Adds the element from back in the queue.
queue_­nam­e.c­lear()
Removes all the elements from the queue.
queue_­nam­e.f­irst()
Returns the first element from the queue.
queue_­nam­e.f­orE­ach­(f(­ele­ment))
Invokes action on each element of this iterable in iteration order.
queue_­nam­e.i­sEmpty
Returns boolean true if the queue is empty else return false.
queue_­nam­e.l­ength
Returns the length of the queue.
queue_­nam­e.r­emo­veF­irst()
Removes the first element from the queue.
queue_­nam­e.r­emo­veL­ast()
Removes the last element from the queue.
Dart also provides the user to manipulate a collection of data in the form of a queue. A queue is a FIFO (First In First Out) data structure where the element that is added first will be deleted first.

Enums

enum variab­le_­name{ // Insert the data members as shown member1, member2, member3, ...., memberN }
Limitation of Enumerated Data Type: It cannot be subclassed or mixed in. It is not possible to explicitly instan­tiate an enum.
Enumerated types (also known as enumer­ations or enums) are primarily used to define named constant values. The enum keyword is used to define an enumer­ation type in Dart. The use case of enumer­ation is to store finite data members under the same type defini­tion.
                               
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          More Cheat Sheets by nimakarimian

          C++ Pointers cookbook Cheat Sheet
          Dart numbers Cheat Sheet