joplin-notes/joplin notion extension/Web Development/Flutter/Dart/Basics.md
2026-11-04 00:33:23 +05:30

3.2 KiB

Basics

  • hello world program
      void main() {
        print('hello world');
      }
    
    
  • data types primitive: int, float,double, char, string, bool example
      void main() {
        String v = 'Rishav';
        print(v);
      }
    
    dynamic : those who can contain any data type var keyword
     void main() {
       var v = 'Rishav';
       var v1 = 10;
       var v2 = 80.12;
       print(v);
       print(v1);
       print(v2);
     }
    
    
    dynamic keyword unlike var whose datatype is fixed when assigned, in dynamic, datatype can be changed anytime   const keyword : once value assigned, it cant be changed   late keyword is non nullable and initialization will take place later and till then it wont recieve null and wait for its value.
import 'dart:io';

void main() {
 const v = 'Rishav';
 var v1;
 v1 = stdin.readLineSync(); // input 12
 dynamic v2 = 80.12;
 v2 = stdin.readLineSync(); //input asd (a string)
 print("Value of v is $v");
 print(v1);
 print(v2);
}

Note : In case of var and const , when we assign value, that value will be assigned during compile time so in future if that assign value (which maybe time or some value from api which can be changed) then in that case dynamic can be used to assign value at run time.   final and const keyword are also similar but in case of objects(arrays) if const is used array is immutable too(means elements inside array cant be changed)

  • comments
    // inline comment
    /*
    *
    *  blocks of code
    */
    
    /// documentation
    
  • operators and other logical statements if/else,switch,for , while , do-while loop and all operators even ternary are C like

 

Type casting

void main() {
  // string to integer
  var one = int.parse('1');
  assert(one == 1);

  one += 10;

  print('one: $one');

  //integer to string
  var oneString = 1.toString();
  oneString += 'Rishav';
  print('oneString: ' + oneString);

  // decimal to string
  var oneString2 = 3.1234.toString();
  var oneString2D = 3.1234.toStringAsFixed(2);
  assert(oneString2D == '3.12');

  print('oneString2D: ' + oneString2D);

  if (oneString2D == '3.12')
    print('correct');
  else {
    print('incorrect');
  }
}

 

Null aware operators Without null aware operator

class Num {
  var num = 10;
}

void main() {
  // var n = Num();
  var n;
  int no = 0;

  if (n != null) {
    no = n.num;
  }

  // print(n) // if var n = Num() then output : Instance of 'Num'
  // print(no) if var n = Num() then output : 10
  print(n); // output : null
  print(no); // output : 0
}
 

With null aware operator

void main() {
  // var n = Num();
  var n;
  int? no = 0;

  // if (n != null) {
  //   no = n.num;
  // }

  no = n?.num;

  // print(n) // if var n = Num() then output : Instance of 'Num'
  // print(no) if var n = Num() then output : 10
  print(n); // output : null
  print(no); // output : null
}

In case of n being null if we dont want to assign null to no of n is null and instead some assign other v no = n?.num ?? 0;

for directly assigning value to a variable whose value is null use ??=

// ??=
void main() {
  int? no;

  print(no ??= 100); // outputs : 100
  print(no); // outputs : 100
}