Sunday 15 February 2015

Dart: Use Futures to asynchronously fill a static var -



Dart: Use Futures to asynchronously fill a static var -

i have defined static var map instances of element. if contains specific key, should utilize value. if key not contains instance should info request , save in static map, other instances utilize it.

class="lang-dart prettyprint-override">static var info = new map(); func() { if (elem.data.containskey(['key']) { list = elem.data['key']; } else { helper.getdata().then((requesteddata) { list = requesteddata; elem.data.addall({ 'key' : requesteddata }); } }

the problem instances go else, since key not contained in map @ moment other instances @ if. need them wait, until info in map.

class="lang-dart prettyprint-override">static var info = new map(); static completer _datacompleter; future<bool> func() { if(_datacompleter == null) { _datacompleter = new completer(); helper.getdata().then((requesteddata) { list = requesteddata; elem.data.addall({ 'key' : requesteddata }); _datacompleter.complete(true); }) } if(_datacompleter.iscompleted) { homecoming new future.value(true); } homecoming _datacompleter.future; }

and phone call like

class="lang-dart prettyprint-override">func().then((success) => /* go on here when `key` in `data` has value.

dart dart-async

No comments:

Post a Comment