Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completer and Future in dart?

Future readData() {
    var completer = new Completer();
    print("querying");
    pool.query('select p.id, p.name, p.age, t.name, t.species '
        'from people p '
        'left join pets t on t.owner_id = p.id').then((result) {
      print("got results");
      for (var row in result) {
        if (row[3] == null) {
          print("ID: ${row[0]}, Name: ${row[1]}, Age: ${row[2]}, No Pets");
        } else {
          print("ID: ${row[0]}, Name: ${row[1]}, Age: ${row[2]}, Pet Name: ${row[3]},     Pet Species ${row[4]}");
        }
      }
      completer.complete(null);
    });
    return completer.future;
  }

The above is an example code taken from github SQLJocky Connector

I would like someone to explain me if possible why is the function which has a completer object created outside the pool.query is then calling a function completer.complete(null).

In short I am not able to understand the part after print executes.

Note:Kindly if possible I would also like to know how is future and Completer used for practical purpose both for DB and non DB operations.

I have explored the following links: Google groups discussion on Future and Completer

and the api reference documentation which is as given below Completer api reference and Future api Reference

like image 694
IBRIT Avatar asked Dec 05 '12 21:12

IBRIT


People also ask

What is a completer in Dart?

A completer allows you to create and manage a future. Once you've instantiated a completer, you can use it to return a future to your API's callers, and when a lengthy asynchronous call returns data or an error, you can complete that future, delivering the result.

What does Future do in Dart?

A Future is used to represent a potential value, or error, that will be available at some time in the future. Receivers of a Future can register callbacks that handle the value or error once it is available.

How is when completed () different from then () in Future?

. whenComplete will fire a function either when the Future completes with an error or not, instead . then will fire a function after the Future completes without an error. This is the asynchronous equivalent of a "finally" block.

What is a completer class?

Completer Programs are different pathways allowing you to fulfill your high school graduation requirements while taking classes you're interested in along the way. Think of completers as you would choosing a major in college.


1 Answers

Correct answer has errors in DartPad, the reason could be Dart version.

error : The argument type 'int' can't be assigned to the parameter type 'Duration'.
error : The argument type '(dynamic) → void' can't be assigned to the parameter type '() → void'.

The following snippet is complement

import 'dart:async';

Future<dynamic> someFutureResult(){
   final c = new Completer();
   // complete will be called in 3 seconds by the timer.
   new Timer(Duration(seconds: 3), () {     
       print("Yeah, this line is printed after 3 seconds");
       c.complete("you should see me final");       
   });
   return c.future;

}

main(){
   someFutureResult().then((dynamic result) => print('$result'));
   print("you should see me first");
}

reslut

you should see me first
Yeah, this line is printed after 3 seconds
you should see me final
like image 86
chunhunghan Avatar answered Oct 11 '22 11:10

chunhunghan