混淆Dart中的未来对象 -

时间:2016-03-03 22:33:43

标签: dart dart-html

知道这里有什么问题吗?

我有一个函数,它通过一个rest调用从服务器接收数据,它返回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和callback的行。然后缓存对象将调用api.function从服务器获取数据,然后提供原始数据,它转到SOMEOBJECT类中的process方法并返回JSON表示的MAP。一旦数据返回缓存管理器以调用then上的future object,它就会失败。有问题的错误。任何的想法? 感谢

1 个答案:

答案 0 :(得分:0)

我只是简单地看了一眼,然后看了

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

缺少退货。可能还有其他问题。