如何将函数传递给Dart中的侦听器?

时间:2013-09-06 00:15:42

标签: function dart

我想将一个函数传递给类构造函数。该函数最终将用于onBlur监听器。有些东西不起作用。 (FWIW,此代码正在“then”函数中执行HttpRequest.getString())

这有效:

query("${_element_id}_input").onBlur.listen((e) {
  InputElement species_id = query("#id_species_id");
  species_id.value = value;
  print(species_id.value);
});

如果我用以下内容替换它,我会收到错误:

  onBlur = ((e) {
    InputElement species_id = query("#id_species_id");
    species_id.value = value;
    print(species_id.value);
  });

  if (onBlur != null) {
    query("#{_element_id}_input").onBlur.listen(onBlur);
  }

如果我命名该函数,我会得到同样的错误。错误是:

Uncaught Error: SyntaxError: Internal Dartium Exception
Stack Trace:
#0      Document.query (file:///mnt/data/b/build/slave/dartium-lucid32-full-trunk/build/src/out/Release/gen/blink/bindings/dart/dart/html/Document.dart:123:128)
#1      query (dart:html:448:49)
#2      AutoComplete.AutoComplete.<anonymous closure> (package:canopy/auto_complete.dart:69:14)
#3      _ThenFuture._zonedSendValue (dart:async/future_impl.dart:371:24)
#4      _TransformFuture._sendValue.<anonymous closure> (dart:async/future_impl.dart:348:48)
#5      _ZoneBase._runInZone (dart:async/zone.dart:74:15)
#6      _ZoneBase._runUnguarded (dart:async/zone.dart:102:22)
#7      _ZoneBase.executeCallback (dart:async/zone.dart:58:23)
#8      _TransformFuture._sendValue (dart:async/future_impl.dart:348:26)
#9      _FutureImpl._setValueUnchecked (dart:async/future_impl.dart:184:26)
#10     _FutureImpl._setValue (dart:async/future_impl.dart:174:23)
#11     _FutureImpl._setOrChainValue (dart:async/future_impl.dart:336:16)
#12     _ThenFuture._zonedSendValue (dart:async/future_impl.dart:377:21)
#13     _TransformFuture._sendValue.<anonymous closure> (dart:async/future_impl.dart:348:48)
#14     _ZoneBase._runInZone (dart:async/zone.dart:82:17)
#15     _ZoneBase._runUnguarded (dart:async/zone.dart:102:22)
#16     _ZoneBase.executeCallback (dart:async/zone.dart:58:23)
#17     _TransformFuture._sendValue (dart:async/future_impl.dart:348:26)
#18     _FutureImpl._setValueUnchecked (dart:async/future_impl.dart:184:26)
#19     _FutureImpl._asyncSetValue.<anonymous closure> (dart:async/future_impl.dart:218:25)
#20     _asyncRunCallback (dart:async/event_loop.dart:9:15)
#21     _createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:8:13)
#22     _Timer._Timer.<anonymous closure> (file:///mnt/data/b/build/slave/dartium-lucid32-full-trunk/build/src/dart/tools/dom/src/native_DOMImplementation.dart:255:17)


Exception: SyntaxError: Internal Dartium Exception
  _DefaultZone.handleUncaughtError.<anonymous closure> (dart:async/zone.dart:146:7)
  _asyncRunCallback (dart:async/event_loop.dart:9:15)
  _asyncRunCallback (dart:async/event_loop.dart:13:7)
  _createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:8:13)
  _Timer._Timer.<anonymous closure> (file:///mnt/data/b/build/slave/dartium-lucid32-full-trunk/build/src/dart/tools/dom/src/native_DOMImplementation.dart:255:17)

1 个答案:

答案 0 :(得分:2)

罪魁祸首不是处理程序函数,而是调用query来添加侦听器:

query("#{_element_id}_input").onBlur.listen(onBlur); - 缺少“$”来解析{_element_id}