Reputation: 5705
I am trying to use firebase package to see if I can use it with my app | https://pub.dartlang.org/packages/firebase
I am trying to covert this code in 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;
});
taken from https://www.firebase.com/docs/web/guide/saving-data.html#section-transactions
My attempt in dart is:
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());
});
}
The correct curVal is returned and the mrn is written correctly to the database. Also the correct value is printed.
However, I get the following error and I am uncertain why:
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
Thanks
Upvotes: 1
Views: 197
Reputation: 657338
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) The value returned from the last chained then()
is returned at 2)
3) Because this is async code a Future<int>
is returned instead of int
.
You call this code like
getMrn().then((result) {
// doSomething with the code here
});
or with await
void someFunc() async { // <== async necessary in order to be able to use await
var result = await getMrn();
}
Upvotes: 2