Dart

/*Literals
true, 'string', 10, 5.5.
*/
// String concats automatically in dart means you don't have to write + (Eg: 'yasho' 'Vardhan' = 'yashoVardhan' )
// To access variable in a string use $var in string
// toString() method
// var.length method
// str.replaceAll(strToReplace, str)
// string.subString(start index, end index[optional]) = slicing
// $ symbol takes only one word and not any method, so use ${var.length}
// Convert String to int or double -> double.parse(str)/int.parse(str)
void main() {  // Runs when code is executed
  // int number = 15;
  // String greet = greeting();
  // String name = 'yash';
  // bool isNight = true;
  // dynamic yash = 'YASHO';  // To change type of variable
  // int age = getAge();
  // num Number;  // This can take value of int and double
  // yash = 30;
  // List names = ['yash', 'singh', 'rajput'];
  // List<String> names = ['yash', 'singh', 'rajput'];
  // names.add('lugia');  // append
  // names.remove('rajput');
  // double angle = 45.78;  // float
  // double number = 1.45e5  Use e for 10 ki power 
  // var variable = 10;  // var Can take value of any DataType.
  // By default the value of any variable is null
  User username = User('yash', 18);
  print(username.name);
}
// void means it will not return anything
String greeting(){ // DataTpye the function will return
  return 'hello';
}

/* FUNCTIONS */
// By default a function always return null
// To pass a function as a parameter to a function use dynamic datatype 
// Arrow function (fat arrow)
int getAge() => 18;
var getSquare = (a) => a*a; 
int area(int l, int b) => l*b;
var cube = (num a) => a*a*a;
int yash(num a) => a*a*a;
int area(int length, int breadth){ // length and breadth are required parameters
  return length * breadth;
}
// Optional parameters = names(String n1, [String n2, String n3])
// Named parameters = names(String n1, {String n2})
// Optional default parameters = names(String n1, int num = 10)


class User {
  // Variables
  // String name = 'yash';
  // int age = 18;
  // methods
  void login(){
    print('Logged in');
  }
  // constructor
  String name;
  int age;
  User(String name, int age){
    this.name = name;
    this.age = age;
  }
}
// Inheritance
class Superuser extends User{
  void publish(){
    print('Published');
  }
  Superuser(String name, int age): super(name, age);
}

MATH module
import 'dart:math' as m;
   FUNCTIONS
acos(num x) → double
Converts x to a double and returns its arc cosine in radians. [...]
asin(num x) → double
Converts x to a double and returns its arc sine in radians. [...]
atan(num x) → double
Converts x to a double and returns its arc tangent in radians. [...]
atan2(num a, num b) → double
A variant of atan. [...]
cos(num radians) → double
Converts radians to a double and returns the cosine of the value. [...]
exp(num x) → double
Converts x to a double and returns the natural exponent, e, to the power x. [...]
log(num x) → double
Converts x to a double and returns the natural logarithm of the value. [...]
max<T extends num>(T a, T b) → T
Returns the larger of two numbers. [...]
min<T extends num>(T a, T b) → T
Returns the lesser of two numbers. [...]
pow(num x, num exponent) → num
Returns x to the power of exponent. [...]
sin(num radians) → double
Converts radians to a double and returns the sine of the value. [...]
sqrt(num x) → double
Converts x to a double and returns the positive square root of the value. [...]
tan(num radians) → double
Converts radians to a double and returns the tangent of the value. [...]

  CONSTANTS
e → const double
Base of the natural logarithms. [...]
2.718281828459045
ln2 → const double
Natural logarithm of 2. [...]
0.6931471805599453
ln10 → const double
Natural logarithm of 10. [...]
2.302585092994046
log2e → const double
Base-2 logarithm of e.
1.4426950408889634
log10e → const double
Base-10 logarithm of e.
0.4342944819032518
pi → const double
The PI constant.
3.1415926535897932
sqrt1_2 → const double
Square root of 1/2.
0.7071067811865476
sqrt2 → const double
Square root of 2.
1.4142135623730951

EXCEPTIONS:
try {
  Money(-5);
}
on IntegerDivisionByZeroException{
 print('zero'); 
}
catch(e){
  e.error();
}
finally{
  print('arceus');
}
}
// Custom Exception
class Hello implements Exception{
  void error(){
    print('Yashovardhan Singh Rajput');
  }
}
void Money(int num){
  if (num <= 0){
    throw new Hello();
  }


Comments