DEV Community

Cover image for Dart Vader, Cheatsheet For complete fundamentals of DART Language
zidniryi
zidniryi

Posted on

Dart Vader, Cheatsheet For complete fundamentals of DART Language

Hello, world ?. Ok on this occasion I will share a cheat sheet for those of you who want to learn the DART Programming Language or want to learn Flutter.

1. The First lesson (Hello World By Force)

// Lesson 1 in dart we must wrapper our code in the main
void main() {
  for (int i = 0; i < 7; i++) {
    print('Hello, World ${i + 1}');
  }
}
Enter fullscreen mode Exit fullscreen mode

2. The Second lesson (Function In Dart Side)

// Define the basic function
// Call the parameter
printNumber(num whichNumber) {
  print('The number is $whichNumber');
}

// Calll on main  function dart
void main() {
  var num = 11;
  printNumber(num);
}
Enter fullscreen mode Exit fullscreen mode

3. The Third lesson (Declare War in Dart)

//How to declare variables on Dart
void main() {
  int age = 20; //This is for declare number
  String name = 'John Doe'; //This is string
  bool isMale = true;
  double height = 168.8;

  // Final type
  const data = 'This is final data';
  final gender = 'Male';

// Common variable
  var country = 'Indonesia';

//  List is synonym with an Array
  var list = [1, 2, 3];

//  Maps is synonym with an Object
  var profile = {
    // A map literal
    // Keys       Values
    'firstName': 'zidni',
    'lasNam': 'ridwan nulmuarif',
    'gender': 'male'
  };
// Print here for test the variables
  print(profile);
}
Enter fullscreen mode Exit fullscreen mode

4. The Fourth lesson (More Function In Dart Spirit)

void main() {
  int num1 = 12;
  int num2 = 8;
  printNumber(num1, num2);

  //Call function 2 (We looping the data)
  var list = [1, 2, 3];
  list.forEach(printElement);

  //Call the function 3
  var add2 = makeAdder(2); // Create a function that adds 2.
  var add4 = makeAdder(4); // Create a function that adds 4.
  assert(add2(3) == 5);
  assert(add4(3) == 7);
}

// function 1
void printNumber(num number1, number2) {
  print(number1 + number2);
}

// function 2
printElement(element) {
  print(element);
}

//Function 3
Function makeAdder(num n) {
  return (num i) => n + i;
}
Enter fullscreen mode Exit fullscreen mode

5. The Fifth lesson (Operator is Weapon)

// Operator on Dart
// unary postfix and argument definition test expr++ expr-- () [] . ?identifier
// unary prefix multiplicative
// additive
// shift
// relational and type test equality
// bitwise AND bitwise XOR bitwise OR logical AND logical OR conditional cascade assignment
// -expr !expr ~expr ++expr --expr * / % ~/
// + -
// << >>
// >= > <= == !=
// &
// ^
// < as
// is is!
// |
// &&
// ||
// expr1 ? expr2 : expr3
// .. =*=/=~/=%=+=-=<<=>>=&=^=|=

void main() {
  var z = 2;
  print(z + z);
  print(z / z);
  print(z * z);
  print(z - z);
  print(z = z);
  print(z != z);
  print(z % z);
  print(z < z);
  print(z > z);
  print(z >= z);
  print(z <= z);
  print(z | z);
}
Enter fullscreen mode Exit fullscreen mode

6. Sixth lesson (Control Your Mind)

// Control Flow

void main() {
  // if else
  bool hasRedHair = true;
  if (hasRedHair) {
    print('Erope');
  } else {
    print('Asia, Africa');
  }

// For loop
  var listData = [1, 2, 3, 4, 5];
  for (var x in listData) {
    print(x);
  }

// While and Do-While
  int number = 0;
  do {
    if (number % 2 == 0) {
      print(number);
    }
    number++;
  } while (number < 10);

  // Switch Case
  var command = 'OPEN';
  switch (command) {
    case 'CLOSED':
      print('Please Open The Door');
      break;
    case 'PENDING':
      print('Door Will Be Open');
      break;
    case 'OPEN':
      print('Door Already Open');
      break;
    case 'DENIED':
      print('You Can`t in because Door is lock');
  }
}
Enter fullscreen mode Exit fullscreen mode

7. The Seventh lesson (Assert You're Dream)

// Use an assert statement to disrupt normal execution if a boolean condition is false.
// You can find examples of assert statements throughout this tour. Here are some more:
void main() {
  String text = 'May the force be with you!';
  assert(text != null);
}
Enter fullscreen mode Exit fullscreen mode

8. The Eighth lesson (Exceptions in goodness)

//  Dart code can throw and catch exceptions. Exceptions are errors indicating that something unexpected happened.
void main() {
  int x = 11;
  int y = 0;
  int res;
  try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException {
    print('Cannot divide by zero');
  }
  exception();
  exceptionWithFinaly();
}

// Try catch with exception
exception() {
  int x = 10;
  int y = 0;
  int res;

  try {
    res = x ~/ y;
  } catch (e) {
    print(e);
  }
}

// With finaly
exceptionWithFinaly() {
  int x = 9;
  int y = 0;
  int res;

  try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException {
    print('Cannot divide by zero');
  } finally {
    print('Finally block executed');
  }
}
Enter fullscreen mode Exit fullscreen mode

9. The Ninth lesson (Classes and Universe Objects)

// Structure of Class in Dart
// class class_name {
//    <fields>
//    <getters/setters>
//    <constructors>
//    <functions>
// }

// Fields βˆ’ A field is any variable declared in a class. Fields represent data pertaining to objects.
// Setters and Getters βˆ’ Allows the program to initialize and retrieve the values of the fields of a class. A default getter/ setter is associated with every class. However, the default ones can be overridden by explicitly defining a setter/ getter.
// Constructors βˆ’ responsible for allocating memory for the objects of the class.
// Functions βˆ’ Functions represent actions an object can take. They are also at times referred to as methods.
void main() {
  Car c = new Car();
  c.desc();
}

class Car {
  // field
  String engine = "B1001";
  String brand = "Ferari";
  String owner = "Zidniryi";
  int yearProd = 2025;
  bool isColorRed = true;

  // function or we can call Method
  void desc() {
    print(engine);
    print(brand);
    print(owner);
    print(yearProd);
    print(isColorRed);
  }
}
Enter fullscreen mode Exit fullscreen mode

10. The Tenth lesson (RegEx the Jedi)

/**
 * A regular expression (regex or regexp for short) is 
 * a special text string for describing a search pattern.
 *  You can think of regular expressions as wildcards on steroids. 
 */

void main() {
  RegExp re = new RegExp(r'(\w+)');
  String str1 = "one two three";
  print('Has match: ${re.hasMatch(str1)}');
  // First match
  Match firstMatch = re.firstMatch(str1);
  print('First match: ${str1.substring(firstMatch.start, firstMatch.end)}');

  //  Iterate all matches
  Iterable matches = re.allMatches(str1);
  matches.forEach((match) {
    print(str1.substring(match.start, match.end));
  });
}
Enter fullscreen mode Exit fullscreen mode

11. The Eleventh lesson (List The Sith)

void main() {
  // Use a List constructor.
  // Or simply use a list literal.
  var fruits = ['apples', 'oranges'];
  // Add to a list.
  fruits.add('kiwis');
  print(fruits);
  print(fruits.length);

  // Add multiple items to a list.
  fruits.addAll(['grapes', 'bananas']);
  print(fruits);

  // Get the list length.
  assert(fruits.length == 5);
  // Remove a single item.
  var appleIndex = fruits.indexOf('apples');
  fruits.removeAt(appleIndex);

  assert(fruits.length == 4);
  print(fruits);
  // Remove all elements from a list.
  fruits.clear();
  assert(fruits.length == 0);
  print(fruits);
}
Enter fullscreen mode Exit fullscreen mode

12. The Twelfth lesson (Date of Yoda)

import 'package:moment/moment.dart';
//For install pubsec.yaml 
//you can see in here => https://dart.dev/tools/pub/pubspec
void main() {
  var now = new DateTime.now();
  var berlinWallFell = new DateTime.utc(1989, 11, 9);
  var moonLanding = DateTime.parse("1969-07-20 20:18:04Z");
  print(now);
  print(moonLanding);
  Moment().add(1, Unit.day);
  var dateStyle = Moment().format('yyyy-MM-dd');
  print(dateStyle);

  print(Moment().subtract(2, Unit.day));
}
}
Enter fullscreen mode Exit fullscreen mode

13. The Thirteenth lesson (Asynchronous Future )

Future<void> getUser() {
  return Future.delayed(Duration(seconds: 3), () => print('Big Data'));
}

void main() {
  getUser();
  print('Fetching user data...');
}
Enter fullscreen mode Exit fullscreen mode

14. The Fourteenth lesson (Asynchronous Future Next Level)

import 'dart:io';

void main() {
  print("Enter your name :");

  // prompt for user input
  String name = stdin.readLineSync();

  // this is a synchronous method that reads user input
  print("Hello Mr. ${name}");
  print("End of main");
}
Enter fullscreen mode Exit fullscreen mode

15. The Fifteenth lesson (Math Attack Clone)

import "dart:math" show pi;

void main() {
  print(pi / 12);
}
}
Enter fullscreen mode Exit fullscreen mode

16. The Sixteenth lesson (Make Lib And Call Skywalker)

// We create library
dart_lib(num1, num2) {
  print(num1 + num2);
}
Enter fullscreen mode Exit fullscreen mode
// We call the library
import '16.0dart_lib.dart';

void main() {
  // call that
  dart_lib(20, 20);
}
Enter fullscreen mode Exit fullscreen mode

That's all for the lesson πŸ˜ˆπŸ˜ˆπŸ˜ˆπŸ˜ˆπŸ˜‡πŸ˜‡πŸ˜‡. May The Force Be With You ...

For clone, You can clone in my GitHub : https://github.com/zidniryi/Dart-Vader

Top comments (0)