Thursday, 15 January 2015

Dart - Help me understand how this code on Dart works -



Dart - Help me understand how this code on Dart works -

please help me understand how code works. , why works way.

variables res in illustration stream<row>.

i want homecoming future<list> processqueryresult method. code works should , result correct.

example 1

class="lang-dart prettyprint-override">future<list> processqueryresult(mysql.results res) { list info = []; res.tolist().then((list<mysql.row> rows) { rows.foreach((mysql.row row) { data.add({name: row.name, email: row.email}); }); }); homecoming new future<list>(() { homecoming data; }); }

in result data contains need data.

but tell me please, why when move part of code

class="lang-dart prettyprint-override">res.tolist().then((list<mysql.row> rows) { rows.foreach((mysql.row row) { data.add({name: row.name, email: row.email}); }); });

to

class="lang-dart prettyprint-override">return new future<list>(() { res.tolist().then((list<mysql.row> rows) { rows.foreach((mysql.row row) { data.add({name: row.name, email: row.email}); }); }); homecoming data; });

finally code (example 2):

class="lang-dart prettyprint-override">future<list> processqueryresult(mysql.results res) { list info = []; homecoming new future<list>(() { res.tolist().then((list<mysql.row> rows) { rows.foreach((mysql.row row) { data.add({name: row.name, email: row.email}); }); }); homecoming data; }); }

then in result empty list.

for test utilize function:

class="lang-dart prettyprint-override">testresult() { mysql.results res = ... // results; processqueryresult(res).then((data) { print(data); }) }

when code of processqueryresult example 1, right data, when code of processqueryresult example 2 - empty list.

tell me please, why happening?

i glad if give link appropriate article or documentation.

class="lang-dart prettyprint-override">return new future<list>(() { res.tolist() // <== 1.) registers future later execution // <== 3.) execute `tolist()` .then((list<mysql.row> rows) { rows.foreach((mysql.row row) { // <== 4.) execute rows.foreach data.add({name: row.name, email: row.email}); }); }); homecoming data; // <== 2.) homecoming info });

3.) executed after code calling above code has finished sync execution

class="lang-dart prettyprint-override">return new future<list>(() { homecoming res.tolist() // <== returning future keeps async parts connected // , 1 executed after other .then((list<mysql.row> rows) { rows.foreach((mysql.row row) { // <== 4.) execute rows.foreach data.add({name: row.name, email: row.email}); }); }) .then((_) data); // <== same here, homecoming info **after** previous statements finished }); });

dart dart-async

No comments:

Post a Comment