Flutter #day27

Photo by Thalia Tran on Unsplash

Flutter #day27

ยท

4 min read

Future, async, and await continued...

synchronous code to demonstrate.

void task1() {
  String task1 = " this is task 1";
  print("task 1 done");
}

void task2() {
  Duration threeseconds = Duration(seconds: 3);
  sleep(threeseconds);
  String task2 = " this is task 2";
  print("task 2 done");
}

void task3() {
  String task3 = " this is task 3";
  print("task 3 done");
}
output

task 1 done
task 2 done
task 3 done

we can see in this that first task 1 is performed and then after a wait of three seconds task 2 and then finally task 3 is performed.

asynchronous code to demonstrate.

import 'dart:io';

void main() {
  performTasks();
}

void performTasks() {
  task1();
  task2();
  task3();
}

void task1() {
  String task1 = " this is task 1";
  print("task 1 done");
}

void task2() {
  Duration threeseconds = Duration(seconds: 3);
  Future.delayed(threeseconds, () {
    String task2 = " this is task 2"; // once three seconds are done then perform these tasks
    print("task 2 done");
  });
}

void task3() {
  String task3 = " this is task 3";
  print("task 3 done");
}
output 

task 1 done
task 3 done
task 2 done

we can see in this that first task 1 is performed and then task 3 is performed and after a wait of three seconds task 2 is performed.

while if we want to have some return from the task that's gonna take time for the execution of the third task, we can try this code:-

import 'dart:io';

void main() {
  performTasks();
}

void performTasks() {
  task1();
  String task2result = task2();
  task3(task2result);
}

void task1() {
  String task1 = " this is task 1";
  print("task 1 done");
}

String task2() {
  Duration threeseconds = Duration(seconds: 3);

  String result = "";

  Future.delayed(threeseconds, () {
    result = " this is task 2";
    print("task 2 done");
  });

  return result;
}

void task3(String task2data) {
  String task3 = " this is task 3";
  print("task 3 done with $task2data");
}

but this gives this output:

output

task 1 done
task 3 done with 
task 2 done

notice that task 3 is incomplete because it doesn't have the output from task 2 at the execution of task 3.

So how to tackle this?

import 'dart:io';

void main() {
  performTasks();
}

void performTasks() async {
  task1();
  String task2result =
      await task2(); // wait till task 2 is complete before returning
  task3(task2result);
}

void task1() {
  String task1 = " this is task 1";
  print("task 1 done");
}

Future task2() async {
  Duration threeseconds = Duration(seconds: 3);

  String result = "";

  await Future.delayed(threeseconds, () {
    result = " this is task 2";
    print("task 2 done");
  });

  return result;
}

void task3(String task2data) {
  String task3 = " this is task 3";
  print("task 3 done with $task2data");
}

now we have used async and await keywords and now we have the required output:

output 

task 1 done
task 2 done
task 3 done with  this is task 2

What exactly are futures?:

future is similar to the dominoes receipt that you receive after ordering your favorite pizza, and are told to wait till the order is completed, so in simple terms, future is not a thing but it's a promise for smth that you are going to get in the future. (similar to promises in javascript. )

we can also be specific for our future, Future<String> rather than the generic Future one.

Stateful Widget lifecycle methods:

  1. init(): if want smth to happen the moment a widget is created and added to the widget tree we'll put that code in init().

  2. build(): if we want smth to happen every time our stateful widget gets rebuilt then we put that code in the build() method.

  3. deactivate(): if we want smth to happen when our stateful widget gets destroyed then we put the code here.

Exception handling in dart:

try and catch blocks similar to other languages.

try
{
    //Do smth that might fail
}
catch(e)
{
    // catch the error here and perform further tasks if your code fails above.
}

null aware opertor:

someVariable ?? defaultValue;

// if the someVariable is null then defaultValue is put

What throws an error, the answer is the throw keyword.

API in Flutter:

API stands for application program interface, it provides developers with standard commands for common operations to not write code from scratch.

ย