我正在尝试使用firebase软件包来查看我是否可以将它与我的应用程序一起使用https://pub.dartlang.org/packages/firebase
我试图在Javascript中隐藏此代码
var upvotesRef = new Firebase('https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes');
upvotesRef.transaction(function (current_value) {
return (current_value || 0) + 1;
});
取自https://www.firebase.com/docs/web/guide/saving-data.html#section-transactions
我对飞镖的尝试是:
int getMrn() {
var testRef = firebaseClient.child('mrn');
return testRef.transaction((curVal) {
return curVal == null ? 1 : curVal + 1;
}).then((result) {
var snapshot = result.snapshot;
print( snapshot.val());
});
}
返回正确的curVal并将mrn正确写入数据库。此外,还会打印正确的值。
但是,我收到以下错误,我不确定原因:
Exception: Uncaught Error: type '_Future' is not a subtype of type 'int' of 'function result'.
Stack Trace:
#0 getMrn (package:epimss_db/firebase_db.dart:26:15)
#1 onMenuItemSelected (package:epimss_lab/components/chempath/nephrology/electrolytes_rqst_form.dart:116:17)
#2 Function.apply (dart:core-patch/function_patch.dart:28)
#3 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#4 invoke (package:smoke/smoke.dart:43:41)
#5 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#6 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#7 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#8 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#9 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)
#10 BlinkEventTarget.dispatchEvent_Callback_1 (dart:_blink:7764)
#11 BlinkEventTarget.dispatchEvent_Callback_1_ (dart:_blink:7765)
#12 EventTarget.dispatchEvent (dart:html:14741)
#13 onMainActionClicked (package:html_components/menu/split_button.dart:34:23)
#14 Function.apply (dart:core-patch/function_patch.dart:28)
#15 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#16 invoke (package:smoke/smoke.dart:43:41)
#17 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#18 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#19 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#20 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#21 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)
1
由于
答案 0 :(得分:2)
Future<int> getMrn() { // <== 3
var testRef = firebaseClient.child('mrn');
return testRef.transaction((curVal) { // <== 2)
return curVal == null ? 1 : curVal + 1;
}).then((result) {
var snapshot = result.snapshot;
print( snapshot.val());
return result; // <== 1)
});
}
1)从最后一个被链接的then()
返回的值在2)返回
3)因为这是异步代码,所以返回Future<int>
而不是int
。
您可以将此代码称为
getMrn().then((result) {
// doSomething with the code here
});
或等待
void someFunc() async { // <== async necessary in order to be able to use await
var result = await getMrn();
}