Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The purpose of function `runZoned` of 'dart:async'

There is a special function runZoned provided by dart:async. The document is here: https://api.dartlang.org/docs/channels/stable/latest/dart_async.html#runZoned

I'm not sure what's the purpose of this function, when will we need it, and how to use it properly?

like image 991
Freewind Avatar asked Feb 03 '14 16:02

Freewind


People also ask

What is async function in Dart?

Here is my summary of asynchronous programming in Dart. Asynchronous function is a function that returns the type of Future. We put await in front of an asynchronous function to make the subsequence lines waiting for that future's result. We put async before the function body to mark that the function support await .

What is runZoned in flutter?

runZoned<R> function Null safetyRuns body in its own zone. Creates a new zone using Zone. fork based on zoneSpecification and zoneValues , then runs body in that zone and returns the result. If onError is provided, it must have one of the types. void Function(Object)

What is the use of async keyword in flutter?

When you await an asynchronous function, the execution of the code within the caller suspends while the async operation is executed. When the operation is completed, the value of what was awaited is contained within a Future object.

What is sync and async in Dart?

This keyword tells Dart to wait till this future is complete and then move on to the next task. For example void main() async { print("a"); //await tells dart to wait till this completes. If it's not used before a future, then dart doesn't wait till the future is completed and executes the next tasks/code.


1 Answers

Look at this code:

import 'dart:async';

void main() {
  fineMethod().catchError((s) {}, test : (e) => e is String);
  badMethod().catchError((s) {}, test : (e) => e is String);
}

Future fineMethod() {
  return new Future(() => throw "I am fine");
}

Future badMethod() {
  new Future(() => throw "I am bad");
  return new Future(() => throw "I am fine");
}

Output

Unhandled exception:
I am bad

Now look at this code:

import 'dart:async';

void main() {
  fineMethod().catchError((s) {}, test : (e) => e is String);

  runZoned(() {
    badMethod().catchError((s) {}, test : (e) => e is String);
  }, onError : (s) {
    print("It's not so bad but good in this also not so big.");
    print("Problem still exists: $s");
  });
}

Future fineMethod() {
  return new Future(() => throw "I am fine");
}

Future badMethod() {
  new Future(() => throw "I am bad");
  return new Future(() => throw "I am fine");
}

Output

It's not so bad but good in this also not so big.
Problem still exists: I am bad

You should strictly avoid using badMethod if this possible.

Only if this not possible you may temporary use runZoned

Also you may use runZoned to simulate sandboxed execution of tasks.

Updated version of the answer:

import 'dart:async';

Future<void> main() async {
  try {
    await fineMethod();
  } catch (e) {
    log(e);
  }

  await runZonedGuarded(() async {
    try {
      await badMethod();
    } catch (e) {
      log(e);
    }
  }, (e, s) {
    print("========");
    print("Unhandled exception, handled by `runZonedGuarded`");
    print("$e");
    print("========");
  });
}

Future badMethod() {
  // Unhandled exceptions
  Future(() => throw "Bad method: bad1");
  Future(() => throw "Bad method: bad2");
  return Future(() => throw "Bad method: fine");
}

Future fineMethod() {
  return Future(() => throw "Fine method: fine");
}

void log(e) {
  print('Handled exception:');
  print('$e');
}

Output:

Handled exception:
Fine method: fine
========
Unhandled exception, handled by `runZonedGuarded`
Bad method: bad1
========
========
Unhandled exception, handled by `runZonedGuarded`
Bad method: bad2
========
Handled exception:
Bad method: fine
like image 115
mezoni Avatar answered Oct 08 '22 22:10

mezoni