Dart4Flutter -01 — variables, types, and functions

Dart4Flutter — 02 — Control flow and anomalies

Dart4Flutter — 03 — Classes and generics

Dart4Flutter — 04 — async and library

Dart 4FLUTTER – gLEANer 01 – Flutter – DART environment setup

Flutter Entry – State management

Introduction to Flutter Example 1

The control flow

If – else

If-else is as simple as any other language.

main(List<String> args) {
  var number = 57;
 
  if (number > 100) {
    print('Large Number');
  } else if (number < 100) {
    print('Small Number');
  } else {
    print('Number is 100'); }}Copy the code

We can use the ternary operator instead of if-else

main(List<String> args) {
  int age = 60;
 
  String status = age < 50 ? "Still young" : "Old Man";
}
Copy the code

cycle

Dart, like most other languages, supports a variety of loops, and the syntax is the same.

The For loop

A typical for loop.

main(List<String> args) {
  for (int i = 0; i < 10; i++) {
    print('$i'); }}Copy the code

The While loop

A typical while loop

main(List<String> args) {
  int i = 0;
  while(i < 10) {
    print('$i'); i++; }}Copy the code

The Do while loop

The classic for do while loop. A typical do while loop.

main(List<String> args) {
  int i = 0;
  do {
    print('$i');
    i++;
  } while (i < 10);
}
Copy the code

Switch

main(List<String> args) {
  int age = 50;
 
  switch(age) {
    case 10:
      print('Too Young.');
      break;
    case 20:
    case 30:
      print('Still Young! ');
      break;
    case 40:
      print('Getting old.');
      break;
    case 50:
      print('You are old! ');
      break; }}Copy the code

Exception handling

Dart handles exceptions using the classic try-catch, using the keyword throw to throw an exception.

An exception is thrown

First, how do I throw an exception

main(List<String> args) {
  divide(10.0);
}
 
divide(int a, int b) {
  if (b == 0) {
    throw new IntegerDivisionByZeroException();
  }
  return a / b;
}
Copy the code

When b variable has a value of 0, throw a built-in exceptions IntegerDivisionByZeroException.

You can also carry string information in an exception.

main(List<String> args) {
  divide(10.0);
}
 
divide(int a, int b) {
  if (b == 0) {
    throw new Exception('Divide by zero');
  }
  return a / b;
}
Copy the code

Catch and handle exceptions

Now let’s focus on catching and handling exceptions.

Certain types of exceptions can be caught with the on keyword, as follows:

main(List<String> args) {
  try {
    divide(10.0);
  } on IntegerDivisionByZeroException {
    print('Division by zero.');
  }
}
 
divide(int a, int b) {
  if (b == 0) {
    throw new IntegerDivisionByZeroException();
  }
  return a / b;
}
Copy the code

If you don’t know what type of exception to throw, or are not sure, you can use a catch block to handle any type of exception.

main(List<String> args) {
  try {
    divide(10.0);
  } on IntegerDivisionByZeroException {
    print('Division by zero.');
  } catch (e) {
    print(e);
  }
}
 
divide(int a, int b) {
  if (b == 0) {
    throw new Exception('Some other exception.');
  }
  return a / b;
}
Copy the code

Finally

Dart also provides a finally block, which is executed whether or not an exception occurs.

main(List<String> args) {
  try {
    divide(10.0);
  } on IntegerDivisionByZeroException {
    print('Division by zero.');
  } catch (e) {
    print(e);
  } finally {
    print('I will always be executed! ');
  }
}
 
divide(int a, int b) {
  if (b == 0) {
    throw new Exception('Some other exception.');
  }
  return a / b;
}
Copy the code

The end of the

Next Dart4Flutter — 03 — classes and generics

reference

Thetechnocafe.com/just-enough…