2016-03-03 45 views
0

任何想法这里有什么问题?对飞镖中未来物体的迷惑 -

我有一个函数,它从服务器接收一个剩余调用的数据,它返回Future对象。

Future _function(){ 
var future = _getWithID('path'); 
    future.then((data) { 
    List<SOMEOBJECT> SOMEOBJECT = data["entities"].map((v) { 
      return new SOMEOBJECT(
       id: v["properties"]["id"], 
       name: titleize(v["properties"]["name"]) 
     ); 
     }).toList(); 
    return new Future.value(SOMEOBJECT.process(SOMEOBJECT)); 
    }); 
} 

模型类SOMEOBJECT

class SOMEOBJECT extends Model { 

    int id; 

    String name; 

    SOMEOBJECT({this.id, this.name}); 

    static Map process(List<String> SOMEOBJECTS) { 
    // map = {name: A, value:[{name:list<String>},{name:list<String>}]} 
    return map; 
    } 
} 

缓存对象,尝试在浏览器缓存

class CacheManager { 
    Map callbacks; 
    Map cache; 

    CacheManager(this.cache){ 
    callbacks = {}; 
    } 

    Future setData(String key, Function updateFunction) { 
    return chrome.storage.local.get(key).then((resp){ 
     cache[key] = resp[key]; 

     return updateFunction().then((data) { 
     chrome.storage.local.set({key: data}); 
     cache[key] = data; 
     }).whenComplete(() { 
     handleCallbacks(key); 
     }); 
    }); 
    } 

    void registerCallback(String key, Function callback) { 
    callbacks[key] = callback; 
    } 

    void handleCallbacks(String key){ 
    if (callbacks[key] != null){ 
     callbacks[key](cache[key]); 
    } 
    } 
} 

所以我之前

cacheManager.registerCallback("SOMEOBJECT", loadSomeOBJECT); 
cacheManager.setData('SOMEOBJECT', api._function); 

这两条线,我得到这个错误:

ERROR 
NoSuchMethodError: method not found: 'then' 
Receiver: null 
Arguments: [Closure: (dynamic) => dynamic] 
#0  Object._noSuchMethod (dart:core-patch/object_patch.dart:42) 
#1  Object.noSuchMethod (dart:core-patch/object_patch.dart:45) 
#2  CacheManager.setData.<anonymous closure> (chrome-extension://ekfcndmmkincdeoolhcebmhcgmkmadip/helpers.dart:27:31) 
#3  _RootZone.runUnary (dart:async/zone.dart:1149) 
#4  _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:551) 
#5  _Future._propagateToListeners (dart:async/future_impl.dart:637) 
#6  _Future._completeWithValue (dart:async/future_impl.dart:424) 
#7  _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:479) 
#8  _microtaskLoop (dart:async/schedule_microtask.dart:41) 
#9  _startMicrotaskLoop (dart:async/schedule_microtask.dart:50) 
#10  _ScheduleImmediateHelper._handleMutation (dart:html:49254) 
#11  MutationObserver._create.<anonymous closure> (dart:html:27525)​ 

这是指从缓存对象setData和回调的行。那么缓存对象将调用api.function从服务器获取数据,然后提供原始数据,然后转到SOMEOBJECT类中的process方法并返回JSON表示的MAP。一旦数据返回缓存管理器调用thenfuture object它失败。与在这个问题上的错误。任何想法? 感谢

回答

0

我刚做了简单的介绍一下,只见

Future _function(){ 
var future = _getWithID('path'); 
    return future.then((data) { 

缺少回报。可能还有其他问题。

+0

这是一个愚蠢的错误,谢谢指出。有时你会对简单的事情失明。 –

+0

这是异步代码的常见错误。你是否拥有“异步”和“等待”。使异步代码更易于使用。 –