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。一旦數據返回緩存管理器調用then
在future object
它失敗。與在這個問題上的錯誤。任何想法? 感謝
這是一個愚蠢的錯誤,謝謝指出。有時你會對簡單的事情失明。 –
這是異步代碼的常見錯誤。你是否擁有「異步」和「等待」。使異步代碼更易於使用。 –