Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- printInteger(int number){
- print('The no. is $number');
- }
- bool isHot(a,hotModel){
- var i = 0;
- var f = true;
- while(i <= hotModel.length){
- if(hotModel[i] != a)
- f = false;
- else
- {
- f = true;
- break;
- }
- i = i + 1;
- }
- return f;
- }
- main(){
- /*
- Variables
- A variable in Dart stores references to a object.
- e.g. var name = "Ameya";
- here var named name is a reference to a String object and the values stored is "Ameya"
- Another way of writing the abovementioned line of code is by explicitly referring the variable name by its Object name as:
- String name = "Ameya";
- Uninitialized variables have a default value of null;
- If an object is not specified to a fixed data type declare it as dynamic.
- dynamic name = "Ameya";
- */
- var number = 12;
- /* Data Types in Dart
- 1. numbers
- int: -2^63 to 2^63-1 on dart vm.
- double: 64bit double-precision floating point no., as specified by the IEEE 754 standard.
- methods like abs(), floor(), etc. from dart:math library can be used.
- */
- //conversions--> String -> int
- var one = int.parse('1');
- assert(one == 1);
- /*
- Assert
- Form: assert(condition, optionalMsg);
- The 1rst arg in assert can be any expression which evaluates to a boolean value.
- If the expression evals to a true value the assertion succeds and continues else it throws an exception.
- Production code ignores assert, it is run in debug mode.
- If you never intend to change the value of a variable declare it as final or constant.
- */
- //String -> double
- var onePointOne = double.parse('1.1');
- assert(onePointOne == 1.1);
- //int -> String
- String oneAsString = 1.toString();
- assert(oneAsString == '1');
- //double -> String
- String piAsString = 3.14159.toStringAsFixed(2);
- assert(piAsString == '3.14');
- /*
- 2. strings
- A Dart string is a sequence of UTF-16 code units. Single/Double quotes are used to create a string.*/
- //some special cases-->
- print('Let\'s escape the string delimiter!!');
- //multiline string.
- print('''
- Mary had a little lamb,
- little lamb,
- little lamb....
- '''
- );
- /*
- 3. booleans
- 4. list 5. sets 6. maps 7. runes(for expressing unicode chars in strings)
- 5. symbols
- Because every variable in Dart refers to an object, constructors can be used to initialize variables.
- * */
- printInteger(number);
- //Spread operator
- var a = ["apple","mango","banana"];
- var b = ["strawberries", ...a];
- print(b[0].split('a'));
- //Higher Order Array Methods:
- b.forEach((item) => print("${b.indexOf(item) + 1} : $item"));
- //var mapped = b.map((item) => return item.t);
- print(mapped);
- //Null Aware spread operator
- var c;
- var d = ["Happy","Sporer",...?c];
- print(d.length);
- //collection if
- //collection for:
- c = [1,2,3];
- d = ["0",for(var i in c) i.toString()];
- print(d);
- print(d.runtimeType);
- /*Set
- A Set is an unordered collection of unique objects of the same data type
- An empty can be declared by:
- var s = <int>{};
- or by
- Set<int> s = {};
- */
- Set<String> friends = {"Ameya","Rachit","Z"};
- //add an element using a add() method, add a set to a set by the addAll() method.
- //Like the List it supports the collection operators and spread operators.
- /*Map
- A Map is an object that associates keys with values.
- The key and values can be any type of Objects. 2 or more key's can hold the same value, but not vice-versa.
- */
- var hotModels = {
- 1 : 'Kylie Jenner',
- 2 : 'Kim Kardasian',
- 3 : 'Khloe Kardasian',
- 4 : 'Gigi Hadid',
- 5 : 'Kendall Jenner',
- 6 : 'Olivia Culpo'
- };
- //Like the List it supports the collection operators and spread operators.
- /* Functions
- Functions are also objects. This means functions can be assigned to variables and functions can be passsed as parameters to other functions.
- */
- print(isHot("Olivia Culpo", hotModels));
- /*Functions can have required parameters and optional parameters
- Optional Parameters are of 2 types: 1. Named and 2. Position
- 1. Named parameters : e.g.
- 2. Positional parameters :
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement